returnNewDocument: true does not return the updated data, rather the previous version of data in Node.js MongoDB native driver version 4.1.3

I upgraded mongodb driver to version 4.1.3, and since returnOriginal is deprecated, I had to replace it with returnNewDocument according to version 4 changes documentation at mongodb native driver github page. This is causing problems for me as findOneAndUpdate is used a lot and I cannot change it to updateOne. Has anyone else faced this issue and is there any solution to this problem?

The code

 const placeholder = {};
 const keys = Object.keys(data);
const values = Object.values(data);
for (let i = 0; i < keys.length; i++) {
   placeholder[keys[i]] = values[i];
}
const update = {
        $set: placeholder,
};
const options = { returnNewDocument: true };

const updatedDoc = await collectionName.findOneAndUpdate(query, update, options);

return updatedDoc .value;

I solved it basically I had to replace “returnNewDocument: true” with the following

{ returnDocument: 'after' }

and it works like a charm

2 Likes

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.