How to Use findOneAndUpdate()
in Mongoose
As the name implies, findOneAndUpdate()
finds the first document that matches a given filter
, applies an update
, and returns the document. By default, findOneAndUpdate()
returns the document as it was before update
was applied.
You should set the new
option to true
to return the document after update
was applied.
const filter = { name: 'Jean-Luc Picard' };
const update = { age: 59 };
// `doc` is the document _after_ `update` was applied because of
// `new: true`
let doc = await Character.findOneAndUpdate(filter, update, {
new: true
doc.name; // 'Jean-Luc Picard'
doc.age; // 59
As an alternative to the new
option, you can also use the returnOriginal
option. returnOriginal: false
is equivalent to new: true
. The returnOriginal
option exists for consistency with the , which has the same option.
With the exception of an unindexed upsert, . That means you can assume the document doesn’t change between when MongoDB finds the document and when it updates the document, unless you’re doing an upsert.
const filter = { name: 'Jean-Luc Picard' };
const update = { age: 59 };
let doc = await Character.findOne({ name: 'Jean-Luc Picard' });
// Document changed in MongoDB, but not in Mongoose
await Character.updateOne(filter, { name: 'Will Riker' });
// This will update `doc` age to `59`, even though the doc changed.
doc.age = 59;
await doc.save();
doc = await Character.findOne();
doc.age; // 59
Using the upsert
option, you can use findOneAndUpdate()
as a find-and- operation. An upsert behaves like a normal findOneAndUpdate()
if it finds a document that matches filter
. But, if no document matches filter
, MongoDB will insert one by combining filter
and update
as shown below.
Mongoose transforms the result of findOneAndUpdate()
by default: it returns the updated document. That makes it difficult to check whether a document was upserted or not. In order to get the updated document and check whether MongoDB upserted a new document in the same operation, you can set the rawResult
flag to make Mongoose return the raw result from MongoDB.
const filter = { name: 'Will Riker' };
const update = { age: 29 };
await Character.countDocuments(filter); // 0
let res = await Character.findOneAndUpdate(filter, update, {
new: true,
upsert: true,
rawResult: true // Return the raw result from the MongoDB driver
});
res.value instanceof Character; // true
// The below property will be `false` if MongoDB upserted a new
// document, and `true` if MongoDB updated an existing object.