R
As MongoDB Github collection documented with respect to Node.js MongoDB Driver API here Find a document and update it in one atomic operation, requires a write lock for the duration of the operation.
Syntax
findOneAndUpdate(filter, update, options, callback)
// Example of a simple findOneAndUpdate operation
var MongoClient = require('mongodb').MongoClient,
test = require('assert');
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
// Get the collection
var col = db.collection('find_one_and_update');
col.insertMany([{a:1, b:1}], {w:1}, function(err, r) {
test.equal(null, err);
test.equal(1, r.result.n);
col.findOneAndUpdate({a:1}
, {$set: {d:1}}
, {
projection: {b:1, d:1}
, sort: {a:1}
, returnOriginal: false
, upsert: true
}
, function(err, r) {
test.equal(null, err);
test.equal(1, r.lastErrorObject.n);
test.equal(1, r.value.b);
test.equal(1, r.value.d);
db.close();
});
});
});
// Example of a simple findOneAndUpdate operation using a Promise.
var MongoClient = require('mongodb').MongoClient,
test = require('assert');
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {
// Get the collection
var col = db.collection('find_one_and_update_with_promise');
col.insertMany([{a:1, b:1}], {w:1}).then(function(r) {
test.equal(1, r.result.n);
col.findOneAndUpdate({a:1}
, {$set: {d:1}}
, {
projection: {b:1, d:1}
, sort: {a:1}
, returnOriginal: false
, upsert: true
}
).then(function(r) {
test.equal(1, r.lastErrorObject.n);
test.equal(1, r.value.b);
test.equal(1, r.value.d);
db.close();
});
});
});
// Example of a simple findOneAndUpdate operation using a Generator and the co module.
var MongoClient = require('mongodb').MongoClient,
co = require('co');
test = require('assert');
co(function*() {
var db = yield MongoClient.connect('mongodb://localhost:27017/test');
// Get the collection
var col = db.collection('find_one_and_update_with_generators');
var r = yield col.insertMany([{a:1, b:1}], {w:1});
test.equal(1, r.result.n);
var r = yield col.findOneAndUpdate({a:1}
, {$set: {d:1}}
, {
projection: {b:1, d:1}
, sort: {a:1}
, returnOriginal: false
, upsert: true
}
);
test.equal(1, r.lastErrorObject.n);
test.equal(1, r.value.b);
test.equal(1, r.value.d);
db.close();
});
As Mongoosejs documented here
Query.prototype.findOneAndUpdate()
Parameters
[query] «Object|Query»
[doc] «Object»
[options] «Object»
[options.rawResult] «Boolean» if true, returns the raw result from
the MongoDB driver
[options.strict] «Boolean|String» overwrites the schema's strict mode
option
[options.multipleCastError] «Boolean» by default, mongoose only
returns the first error that occurred in casting the query. Turn on
this option to aggregate all the cast errors.
[options.lean] «Object» if truthy, mongoose will return the document
as a plain JavaScript object rather than a mongoose document. See
Query.lean().
[callback] «Function» optional params are (error, doc), unless
rawResult is used, in which case params are (error, writeOpResult)
This function triggers the following middleware.
findOneAndUpdate()
Available options
new: bool - if true, return the modified document rather than the
original. defaults to false (changed in 4.0)
upsert: bool - creates the object if it doesn't exist. defaults to
false.
fields: {Object|String} - Field selection. Equivalent to
.select(fields).findOneAndUpdate()
sort: if multiple docs are found by the conditions, sets the sort
order to choose which doc to update
maxTimeMS: puts a time limit on the query - requires mongodb >= 2.6.0
runValidators: if true, runs update validators on this command.
Update validators validate the update operation against the model's
schema.
setDefaultsOnInsert: if this and upsert are true, mongoose will apply
the defaults specified in the model's schema if a new document is
created. This option only works on MongoDB >= 2.4 because it relies
on MongoDB's $setOnInsert operator.
rawResult: if true, returns the raw result from the MongoDB driver
context (string) if set to 'query' and runValidators is on, this will
refer to the query in custom validator functions that update
validation runs. Does nothing if runValidators is false.
Callback Signature function(error, doc) { // error: any errors that
occurred // doc: the document before updates are applied if new:
false, or after updates if new = true }
Examples
query.findOneAndUpdate(conditions, update, options, callback) // executes
query.findOneAndUpdate(conditions, update, options) // returns Query
query.findOneAndUpdate(conditions, update, callback) // executes
query.findOneAndUpdate(conditions, update) // returns Query
query.findOneAndUpdate(update, callback) // returns Query
query.findOneAndUpdate(update) // returns Query
query.findOneAndUpdate(callback) // executes
query.findOneAndUpdate() // returns Query