Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get updated document back from the findOneAndUpdate method?

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", 
like image 940
Kanishka Panamaldeniya Avatar asked Feb 25 '16 11:02

Kanishka Panamaldeniya


People also ask

What does findOneAndUpdate return?

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.

What does update in MongoDB return?

collection. update() will only report the number of documents that were affected to its own callback.

What does findByIdAndUpdate return?

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.

What is the difference between findOneAndUpdate and updateOne?

findOneAndUpdate returns a document whereas updateOne does not (it just returns the _id if it has created a new document).


2 Answers

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.

like image 185
robertklep Avatar answered Oct 20 '22 17:10

robertklep


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/)

like image 45
Naveen Venkatesh Avatar answered Oct 20 '22 16:10

Naveen Venkatesh