findOneAndUpdate used with returnNewDocument:true returns the Original document MongoDB

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.


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.


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 
                  }      */       
        }
    });