I am using MongoDB with node js , i used npm install mongodb
I want to update an existing document and return the updated document , the document is updated correctly . but it returns the old document means the original document before update . i have used the returnNewDocument:true
parameter but no use .
var filter = { '_id': object_id }, update = { $set: { "status" : data["status"] }, $push: { "statusHistory": { $each: [{ status:data["status"],statusChangedTime:data["statusChangedTime"],comment:data["comment"]}], $position:0, } }, } ,options = { //upsert: false, //multi: false, returnNewDocument: true }; col.findOneAndUpdate(filter, update, options,function(err, res) { if (err) { console.log(err); }else { console.log(res); } });
the response is
{ lastErrorObject: { updatedExisting: true, n: 1 }, value: { //original document }, ok: 1 }
when i directly go to mongoDB through terminal and try
db.MyCollection.find().pretty();
the document is updated correctly, it just returns the original instead of updated one.
Stuck here for 2 hours, any help is appreciated
in package.json
"mongodb": "^2.1.4",
By default, findOneAndUpdate() returns the document as it was before update was applied. You should set the new option to true to return the document after update was applied.
collection. update() will only report the number of documents that were affected to its own callback.
Mongoose | findByIdAndUpdate() Function The findByIdAndUpdate() function is used to find a matching document, updates it according to the update arg, passing any options, and returns the found document (if any) to the callback.
findOneAndUpdate returns a document whereas updateOne does not (it just returns the _id if it has created a new document).
The Node.js driver documentation doesn't mention a returnNewDocument
option for findOneAndUpdate()
(which is an option for the MongoDB shell command with the same name).
Instead, it mentions an option called returnOriginal
, which defaults to true
. Try using that option, setting it to false
to return the updated document instead of the original.
To get the updated document after performing the update operation we need to use the option "returnDocument" : "after"
along with "returnOriginal" : false
.
As per the latest mongodb node driver v3.6 documentation, the usage of returnOriginal is deprecated. But when I try only including "returnDocument" : 'after'
without "returnOriginal":false
its returning the original record instead of the updated record. Using them both is giving the desired output of updated record instead of the original record. (source : http://mongodb.github.io/node-mongodb-native/3.6/api/Collection.html#findOneAndUpdate)
The option "returnNewDocument" : true
is a mongo shell option and may not work in the node driver as per the official mongodb documentation ( mentioned here https://docs.mongodb.com/manual/reference/method/db.collection.findOneAndUpdate/)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With