collection.findOneAndUpdate
Find a document and update it in one atomic operation, requires a write lock for the duration of the operation.
Arguments
query
(String|ObjectId|Object)update
(Object): Update operations to be performed on the document. As written in MongoDB docs, you need to specify an atomic operator here (like a$set
,$unset
, or$rename
).[
options
] (Object|String|Array): If theoptions
is a string, it will be parsed as the fields to select.
options.returnOriginal
is default to false
, while mongodb
set it to true
for undefined
.
if options.replaceOne
is true
, it will work like findOneAndReplace.
- [
callback
] (function)
Returns
A promise.
Example
users.findOneAndUpdate({name: 'foo'}, { $set: { name: 'bar'} }).then((updatedDoc) => {})
Note that you can also use the monk-middleware-wrap-non-dollar-update middleware, which will automatically put the $set
operator on the update
argument:
db.addMiddleware(require('monk-middleware-wrap-non-dollar-update'))
users.findOneAndUpdate({name: 'foo'}, { name: 'bar'}).then((updatedDoc) => {})