findOneAndUpdate
noun
say it: “FYND WUN and UP-dayt” /ˈfaɪnd ˈwʌn ənd ˈʌpdeɪt/
A MongoDB method that changes one matching document and returns it, as it was before the change or as it is after.
What does findOneAndUpdate do?
findOneAndUpdate changes the first matching document and gives that document back in the same step. By default you get it as it was before the change; returnDocument: "after" gives the changed version.
db.recipes.findOneAndUpdate(
{ _id: 4 },
{ $inc: { saves: 1 } },
{ returnDocument: "after" }
);
// { _id: 4, name: 'Chickpea Curry', saves: 14, ... }
In a sentence
- “Without returnDocument set to after, findOneAndUpdate hands back the document as it was before the change.”
- “The save button calls findOneAndUpdate so the page can show the new save count right away.”
Level: Journeyman · More MongoDB terms at this level
Updated