Reputation: 1058
const query = { uuid: '12212121212112' };
const values = { $set: {name:'av'} };
const result = await databaseConnection.collection('person');
let data = await result.findOneAndUpdate(query, values, { new: true });
console.log(data) //old document getting printed
This is my code. In here I am using {new: true}
but somehow still its returning the old document instead of the updated one.
Configuration is-
"mongodb": "^3.5.7",
"mongoose": "^5.9.17",
node version- v14.17.3
What is wrong with this?
P.S.- In another project with the same configuration it's working fine.
Upvotes: 2
Views: 983
Reputation: 688
try this way...
let result = await db1.updateOne({ _id: 'XXXXXXXXX' }, { $set: { name: 'My name' } }, { returnNewDocument: true });
console.log(result.data.value) //new document getting printed
Also check this working example
https://mongoplayground.net/p/hJgsH7V0Mps
Upvotes: 0
Reputation: 73
Replace line no.4 with below:
let data = await result.findOneAndUpdate(query, values, { returnOriginal: false, returnDocument: "after" });
Works for me ! Thanks
Upvotes: 1