How to update array elements in MongoDB instead of creating new one
I have problem with my code. I want to update document, which looks like this:
{
"_id": {
"$oid": "5bf2ad5a0d46b81798232cf9"
},
"manufacturer": "VW",
"model": "Golf ",
"VIN": "WVWZZZ1J212566691",
"doors": 3,
"class": "compact",
"reservations": [
{
"_id": {
"$oid": "5bf2ad5a0d46b81798232cfa"
},
"pick_up_date": {
"$date": "2014-10-13T09:13:00.000Z"
},
"drop_off_date": {
"$date": "2014-10-14T09:13:00.000Z"
},
"user_id": {
"$oid": "5bec00bdfb6fc005dcd5423b"
}
}
],
"createdAt": {
"$date": "2018-11-19T12:32:26.665Z"
},
"updatedAt": {
"$date": "2018-11-19T12:32:26.665Z"
},
"__v": 0
}
I want to add new reservation to array Reservations. My function:
const createReservationForCarId = ({ body , params }, res, next) =>
Carmodel.findById(params.id)
.then(notFound(res))
.then((carmodel) => carmodel ? Object.assign(carmodel, body).save() : null)
.then((carmodel) => carmodel ? carmodel.view(true) : null)
.then(success(res))
.catch(next)
But when I'm trying to update through:
router.put('/:id/reservation',
createReservationForCarId)
Body:
{
"reservations":[
{
"pick_up_date" : "October 13, 2017 11:13:00",
"drop_off_date": "October 14, 2017 11:13:00",
"user_id":"5bec00bdfb6fc005dcd5423b"
}
]
}
Mongo instead of update my old document is creating new one with only one reservation gave in above body.
What should I do to only update existing document, not creating new one?
arrays mongodb updates
add a comment |
I have problem with my code. I want to update document, which looks like this:
{
"_id": {
"$oid": "5bf2ad5a0d46b81798232cf9"
},
"manufacturer": "VW",
"model": "Golf ",
"VIN": "WVWZZZ1J212566691",
"doors": 3,
"class": "compact",
"reservations": [
{
"_id": {
"$oid": "5bf2ad5a0d46b81798232cfa"
},
"pick_up_date": {
"$date": "2014-10-13T09:13:00.000Z"
},
"drop_off_date": {
"$date": "2014-10-14T09:13:00.000Z"
},
"user_id": {
"$oid": "5bec00bdfb6fc005dcd5423b"
}
}
],
"createdAt": {
"$date": "2018-11-19T12:32:26.665Z"
},
"updatedAt": {
"$date": "2018-11-19T12:32:26.665Z"
},
"__v": 0
}
I want to add new reservation to array Reservations. My function:
const createReservationForCarId = ({ body , params }, res, next) =>
Carmodel.findById(params.id)
.then(notFound(res))
.then((carmodel) => carmodel ? Object.assign(carmodel, body).save() : null)
.then((carmodel) => carmodel ? carmodel.view(true) : null)
.then(success(res))
.catch(next)
But when I'm trying to update through:
router.put('/:id/reservation',
createReservationForCarId)
Body:
{
"reservations":[
{
"pick_up_date" : "October 13, 2017 11:13:00",
"drop_off_date": "October 14, 2017 11:13:00",
"user_id":"5bec00bdfb6fc005dcd5423b"
}
]
}
Mongo instead of update my old document is creating new one with only one reservation gave in above body.
What should I do to only update existing document, not creating new one?
arrays mongodb updates
docs.mongodb.com/manual/reference/method/db.collection.update
– Ferus7
Nov 19 '18 at 14:03
@Ferus7 take a look at comment below.
– Matthew
Nov 19 '18 at 16:50
add a comment |
I have problem with my code. I want to update document, which looks like this:
{
"_id": {
"$oid": "5bf2ad5a0d46b81798232cf9"
},
"manufacturer": "VW",
"model": "Golf ",
"VIN": "WVWZZZ1J212566691",
"doors": 3,
"class": "compact",
"reservations": [
{
"_id": {
"$oid": "5bf2ad5a0d46b81798232cfa"
},
"pick_up_date": {
"$date": "2014-10-13T09:13:00.000Z"
},
"drop_off_date": {
"$date": "2014-10-14T09:13:00.000Z"
},
"user_id": {
"$oid": "5bec00bdfb6fc005dcd5423b"
}
}
],
"createdAt": {
"$date": "2018-11-19T12:32:26.665Z"
},
"updatedAt": {
"$date": "2018-11-19T12:32:26.665Z"
},
"__v": 0
}
I want to add new reservation to array Reservations. My function:
const createReservationForCarId = ({ body , params }, res, next) =>
Carmodel.findById(params.id)
.then(notFound(res))
.then((carmodel) => carmodel ? Object.assign(carmodel, body).save() : null)
.then((carmodel) => carmodel ? carmodel.view(true) : null)
.then(success(res))
.catch(next)
But when I'm trying to update through:
router.put('/:id/reservation',
createReservationForCarId)
Body:
{
"reservations":[
{
"pick_up_date" : "October 13, 2017 11:13:00",
"drop_off_date": "October 14, 2017 11:13:00",
"user_id":"5bec00bdfb6fc005dcd5423b"
}
]
}
Mongo instead of update my old document is creating new one with only one reservation gave in above body.
What should I do to only update existing document, not creating new one?
arrays mongodb updates
I have problem with my code. I want to update document, which looks like this:
{
"_id": {
"$oid": "5bf2ad5a0d46b81798232cf9"
},
"manufacturer": "VW",
"model": "Golf ",
"VIN": "WVWZZZ1J212566691",
"doors": 3,
"class": "compact",
"reservations": [
{
"_id": {
"$oid": "5bf2ad5a0d46b81798232cfa"
},
"pick_up_date": {
"$date": "2014-10-13T09:13:00.000Z"
},
"drop_off_date": {
"$date": "2014-10-14T09:13:00.000Z"
},
"user_id": {
"$oid": "5bec00bdfb6fc005dcd5423b"
}
}
],
"createdAt": {
"$date": "2018-11-19T12:32:26.665Z"
},
"updatedAt": {
"$date": "2018-11-19T12:32:26.665Z"
},
"__v": 0
}
I want to add new reservation to array Reservations. My function:
const createReservationForCarId = ({ body , params }, res, next) =>
Carmodel.findById(params.id)
.then(notFound(res))
.then((carmodel) => carmodel ? Object.assign(carmodel, body).save() : null)
.then((carmodel) => carmodel ? carmodel.view(true) : null)
.then(success(res))
.catch(next)
But when I'm trying to update through:
router.put('/:id/reservation',
createReservationForCarId)
Body:
{
"reservations":[
{
"pick_up_date" : "October 13, 2017 11:13:00",
"drop_off_date": "October 14, 2017 11:13:00",
"user_id":"5bec00bdfb6fc005dcd5423b"
}
]
}
Mongo instead of update my old document is creating new one with only one reservation gave in above body.
What should I do to only update existing document, not creating new one?
arrays mongodb updates
arrays mongodb updates
edited Nov 19 '18 at 14:20
asked Nov 19 '18 at 13:50


Matthew
153
153
docs.mongodb.com/manual/reference/method/db.collection.update
– Ferus7
Nov 19 '18 at 14:03
@Ferus7 take a look at comment below.
– Matthew
Nov 19 '18 at 16:50
add a comment |
docs.mongodb.com/manual/reference/method/db.collection.update
– Ferus7
Nov 19 '18 at 14:03
@Ferus7 take a look at comment below.
– Matthew
Nov 19 '18 at 16:50
docs.mongodb.com/manual/reference/method/db.collection.update
– Ferus7
Nov 19 '18 at 14:03
docs.mongodb.com/manual/reference/method/db.collection.update
– Ferus7
Nov 19 '18 at 14:03
@Ferus7 take a look at comment below.
– Matthew
Nov 19 '18 at 16:50
@Ferus7 take a look at comment below.
– Matthew
Nov 19 '18 at 16:50
add a comment |
3 Answers
3
active
oldest
votes
You can try the findAndModify instead of findById in your code. Check out the syntax here
I have modified my method and changed findById(), to findByIdAndUpdate(), but now it modifies existing document, in detail existing array object in Reservations. I want to add new object to array, not modify existing. mongoosejs.com/docs/api.html#model_Model.findByIdAndUpdate How to fix this? @Susmita Bhattacharjee
– Matthew
Nov 19 '18 at 16:43
You can use $push property to append new elements to your reservation array. Take a look at the accepted answer of this. In this example, try replacing{ "$push": { "childrens": employee._id }}
with something like{ "$push": { "reservations": {"newelement1": "newvalue1", "newelement2":"newvalue2"} }}
– Susmita Bhattacharjee
Nov 20 '18 at 6:54
add a comment |
I suggest you use $push operator of Mongoose which will append your object to the existing array and it will not create a new one.
Check out this link:-
https://github.com/Automattic/mongoose/issues/4338
add a comment |
I haven't used mongoose but they have documentation for array manipulation.
See here:
https://mongoosejs.com/docs/api.html#mongoosearray_MongooseArray-push
Also here is the related mongo doc for array manipulation:
https://docs.mongodb.com/manual/reference/operator/update/push/
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53376077%2fhow-to-update-array-elements-in-mongodb-instead-of-creating-new-one%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can try the findAndModify instead of findById in your code. Check out the syntax here
I have modified my method and changed findById(), to findByIdAndUpdate(), but now it modifies existing document, in detail existing array object in Reservations. I want to add new object to array, not modify existing. mongoosejs.com/docs/api.html#model_Model.findByIdAndUpdate How to fix this? @Susmita Bhattacharjee
– Matthew
Nov 19 '18 at 16:43
You can use $push property to append new elements to your reservation array. Take a look at the accepted answer of this. In this example, try replacing{ "$push": { "childrens": employee._id }}
with something like{ "$push": { "reservations": {"newelement1": "newvalue1", "newelement2":"newvalue2"} }}
– Susmita Bhattacharjee
Nov 20 '18 at 6:54
add a comment |
You can try the findAndModify instead of findById in your code. Check out the syntax here
I have modified my method and changed findById(), to findByIdAndUpdate(), but now it modifies existing document, in detail existing array object in Reservations. I want to add new object to array, not modify existing. mongoosejs.com/docs/api.html#model_Model.findByIdAndUpdate How to fix this? @Susmita Bhattacharjee
– Matthew
Nov 19 '18 at 16:43
You can use $push property to append new elements to your reservation array. Take a look at the accepted answer of this. In this example, try replacing{ "$push": { "childrens": employee._id }}
with something like{ "$push": { "reservations": {"newelement1": "newvalue1", "newelement2":"newvalue2"} }}
– Susmita Bhattacharjee
Nov 20 '18 at 6:54
add a comment |
You can try the findAndModify instead of findById in your code. Check out the syntax here
You can try the findAndModify instead of findById in your code. Check out the syntax here
answered Nov 19 '18 at 14:43
Susmita Bhattacharjee
615
615
I have modified my method and changed findById(), to findByIdAndUpdate(), but now it modifies existing document, in detail existing array object in Reservations. I want to add new object to array, not modify existing. mongoosejs.com/docs/api.html#model_Model.findByIdAndUpdate How to fix this? @Susmita Bhattacharjee
– Matthew
Nov 19 '18 at 16:43
You can use $push property to append new elements to your reservation array. Take a look at the accepted answer of this. In this example, try replacing{ "$push": { "childrens": employee._id }}
with something like{ "$push": { "reservations": {"newelement1": "newvalue1", "newelement2":"newvalue2"} }}
– Susmita Bhattacharjee
Nov 20 '18 at 6:54
add a comment |
I have modified my method and changed findById(), to findByIdAndUpdate(), but now it modifies existing document, in detail existing array object in Reservations. I want to add new object to array, not modify existing. mongoosejs.com/docs/api.html#model_Model.findByIdAndUpdate How to fix this? @Susmita Bhattacharjee
– Matthew
Nov 19 '18 at 16:43
You can use $push property to append new elements to your reservation array. Take a look at the accepted answer of this. In this example, try replacing{ "$push": { "childrens": employee._id }}
with something like{ "$push": { "reservations": {"newelement1": "newvalue1", "newelement2":"newvalue2"} }}
– Susmita Bhattacharjee
Nov 20 '18 at 6:54
I have modified my method and changed findById(), to findByIdAndUpdate(), but now it modifies existing document, in detail existing array object in Reservations. I want to add new object to array, not modify existing. mongoosejs.com/docs/api.html#model_Model.findByIdAndUpdate How to fix this? @Susmita Bhattacharjee
– Matthew
Nov 19 '18 at 16:43
I have modified my method and changed findById(), to findByIdAndUpdate(), but now it modifies existing document, in detail existing array object in Reservations. I want to add new object to array, not modify existing. mongoosejs.com/docs/api.html#model_Model.findByIdAndUpdate How to fix this? @Susmita Bhattacharjee
– Matthew
Nov 19 '18 at 16:43
You can use $push property to append new elements to your reservation array. Take a look at the accepted answer of this. In this example, try replacing
{ "$push": { "childrens": employee._id }}
with something like { "$push": { "reservations": {"newelement1": "newvalue1", "newelement2":"newvalue2"} }}
– Susmita Bhattacharjee
Nov 20 '18 at 6:54
You can use $push property to append new elements to your reservation array. Take a look at the accepted answer of this. In this example, try replacing
{ "$push": { "childrens": employee._id }}
with something like { "$push": { "reservations": {"newelement1": "newvalue1", "newelement2":"newvalue2"} }}
– Susmita Bhattacharjee
Nov 20 '18 at 6:54
add a comment |
I suggest you use $push operator of Mongoose which will append your object to the existing array and it will not create a new one.
Check out this link:-
https://github.com/Automattic/mongoose/issues/4338
add a comment |
I suggest you use $push operator of Mongoose which will append your object to the existing array and it will not create a new one.
Check out this link:-
https://github.com/Automattic/mongoose/issues/4338
add a comment |
I suggest you use $push operator of Mongoose which will append your object to the existing array and it will not create a new one.
Check out this link:-
https://github.com/Automattic/mongoose/issues/4338
I suggest you use $push operator of Mongoose which will append your object to the existing array and it will not create a new one.
Check out this link:-
https://github.com/Automattic/mongoose/issues/4338
edited Nov 20 '18 at 6:46
answered Nov 20 '18 at 6:38


Sunny Parekh
1587
1587
add a comment |
add a comment |
I haven't used mongoose but they have documentation for array manipulation.
See here:
https://mongoosejs.com/docs/api.html#mongoosearray_MongooseArray-push
Also here is the related mongo doc for array manipulation:
https://docs.mongodb.com/manual/reference/operator/update/push/
add a comment |
I haven't used mongoose but they have documentation for array manipulation.
See here:
https://mongoosejs.com/docs/api.html#mongoosearray_MongooseArray-push
Also here is the related mongo doc for array manipulation:
https://docs.mongodb.com/manual/reference/operator/update/push/
add a comment |
I haven't used mongoose but they have documentation for array manipulation.
See here:
https://mongoosejs.com/docs/api.html#mongoosearray_MongooseArray-push
Also here is the related mongo doc for array manipulation:
https://docs.mongodb.com/manual/reference/operator/update/push/
I haven't used mongoose but they have documentation for array manipulation.
See here:
https://mongoosejs.com/docs/api.html#mongoosearray_MongooseArray-push
Also here is the related mongo doc for array manipulation:
https://docs.mongodb.com/manual/reference/operator/update/push/
answered Nov 19 '18 at 22:02
derekjgrove
12
12
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53376077%2fhow-to-update-array-elements-in-mongodb-instead-of-creating-new-one%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
docs.mongodb.com/manual/reference/method/db.collection.update
– Ferus7
Nov 19 '18 at 14:03
@Ferus7 take a look at comment below.
– Matthew
Nov 19 '18 at 16:50