following the official manual,
db.students2.findOneAndUpdate(
{ _id : 1 },
[ { $set: { "total" : { $sum: "$grades.grade" } } } ], // The $set stage is an alias for ``$addFields`` stage
{ returnNewDocument: true }
)
My implementation,
const usersCollection = db().collection("users"); // db() is exposed via a module
usersCollection.findOneAndUpdate(
{ _id : new ObjectID (this.data._id)},
{ $set: {
total : this.data.updatedTotal
}
} ,
{ returnNewDocument: true }
)
despite the { returnNewDocument: true }
option, it still returns the old document
But, when I query the collection again,
usersCollection.findOne({_id: new ObjectID()}); // returns the updated document
it returns the updated document. Which means that the db.collection.findOneAndUpdate()
worked.