-
Why MongoDB on Nodejs Shows Different Results
over 6 years ago
-
over 6 years ago
Hi,
The problem seems with the way you have implemented it. You should use `findByIdAndUpdate` method, instead of searching it first and then updating the comments subdocument. The solution goes as follows:-
Camp.findByIdAndUpdate( id, {$push: {"comments.type": req.body.comments.type}}, {safe: true, upsert: false}, function(err, model) { if(err) console.log('error occured!') console.log('Saved successfully..'); } );
upsert
boolean Optional. If set to true
, creates a new document when no document matches the query criteria. The default value isfalse
, which does not insert a new document when no match is found.
Some helpful links
https://docs.mongodb.com/manual/reference/method/db.collection.update/index.html
https://medium.com/stackfame/how-to-push-or-pop-items-into-mongodb-document-array-via-mongoose-in-node-js-express-js-91b7bbd0d218 -
1 Answer(s)