findOneAndUpdate used with returnNewDocument:true returns the Original document MongoDB

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",

Answers:

Answer

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.

Answer

If you are seeing this latest 2018, neither returnNewDocument:true nor returnOriginal: false, works. You have to instead set a property called new to true as in {.., new: true} in the option object for your query.

Answer

The NodeJS MongoDB driver has different arguments compared to the native MongoDB shell for the command findOneAndUpdate(). If you are using "mongodb": "^2.1.4" then use

returnOriginal: false

instead

returnNewDocument: true

.

Let's see below code:

db.collection('user_setting').findOneAndUpdate({user_id: data.user_id}, {$set: data}, {projection: dbConfig.userSetting, returnOriginal: false}, function (err, res) {
        if (err) {
            callback({'error': 1, 'message': 'Internal server error! ' + err, 'data': null, 'status': 500});
        }   else {
                 console.log(res);
                /* { lastErrorObject: { updatedExisting: true, n: 1 },
                      value: 
                           { user_id: 1,
                             notification_alert: 1,
                             notification_sound: 1,
                             user_setting_id: 2 
                            },
                       ok: 1 
                  }      */       
        }
    });
Answer

If anyone else ran into this issue when using { returnOriginal: false } within a Mongoose setup:

Mongoose uses { new: true } instead of { returnOriginal: false }.

The feature is referenced here: Mongoose: findOneAndUpdate doesn't return updated document and within the Mongoose docs:

Options:

new: bool - if true, return the modified document rather than the original. defaults to false (changed in 4.0)

So when using findOneAndUpdate add new: true to the method options:

...

const options = {
  new: true
};

col.findOneAndUpdate(filter, update, options, function (err, res) {
  if (err) {
    console.log(err);
  } else {
    console.log(res);
  }
});
Answer

I am using mongoose: 5.8.9

with these lines below

const option = { new: true }
const account = await Account.findOneAndUpdate(filter, update, option)

and this is working

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.