Document.prototype.save()
Parameters
[options] «Object» options optional options
[options.session=null] «Session» the session associated with this save operation. If not specified, defaults to the document’s associated session.
[options.safe] «Object» (DEPRECATED) overrides schema’s safe option. Use the
w
option instead.[options.validateBeforeSave] «Boolean» set to false to save without validating.
[options.validateModifiedOnly=false] «Boolean» If
true
, Mongoose will only validate modified paths, as opposed to modified paths andrequired
paths.[options.w] «Number|String» set the write concern. Overrides the schema-level
writeConcern
option[options.j] «Boolean» set to true for MongoDB to wait until this
save()
has been journaled before resolving the returned promise. Overrides the schema-levelwriteConcern
option[options.wtimeout] «Number» sets a timeout for the write concern. Overrides the schema-level
writeConcern
option.[options.checkKeys=true] «Boolean» the MongoDB driver prevents you from saving keys that start with ‘$’ or contain ‘.’ by default. Set this option to
false
to skip that check. See restrictions on field names[options.timestamps=true] «Boolean» if
false
and timestamps are enabled, skip timestamps for thissave()
.[fn] «Function» optional callback
Returns:
- «Promise,undefined» Returns undefined if used with callback or a Promise otherwise.
Saves this document by inserting a new document into the database if document.isNew is true
, or sends an updateOne operation only with the modifications to the database, it does not replace the whole document in the latter case.
Example:
product.sold = Date.now();
product = await product.save();
If save is successful, the returned promise will fulfill with the document saved.
Example:
const newProduct = await product.save();
newProduct === product; // true