Model.replaceOne()
Parameters
- filter «Object»
- doc «Object»
[options] «Object» optional see
Query.prototype.setOptions()
[options.strict] «Boolean|String» overwrites the schema’s strict mode option
[options.upsert=false] «Boolean» if true, and no documents found, insert a new document
[options.writeConcern=null] «Object» sets the write concern for replica sets. Overrides the schema-level write concern
[options.omitUndefined=false] «Boolean» If true, delete any properties whose value is
undefined
when casting an update. In other words, if this is set, Mongoose will deletebaz
from the update inModel.updateOne({}, { foo: 'bar', baz: undefined })
before sending the update to the server.[options.timestamps=null] «Boolean» If set to
false
and schema-level timestamps are enabled, skip timestamps for this update. Does nothing if schema-level timestamps are not set.[callback] «Function»
function(error, res) {}
whereres
has 3 properties:n
,nModified
,ok
.
Returns:
- «Query»
Same as update()
, except MongoDB replace the existing document with the given document (no atomic operators like $set
).
Example:
const res = await Person.replaceOne({ _id: 24601 }, { name: 'Jean Valjean' });
res.n; // Number of documents matched
res.nModified; // Number of documents modified
This function triggers the following middleware.
replaceOne()