deno.land / x / mongoose@6.7.5 / docs / defaults.md
Your schemas can define default values for certain paths. If you create a new document without that path set, the default will kick in.
Note: Mongoose only applies a default if the value of the path is
strictly undefined
.
[require:Declaring defaults in your schema]
You can also set the default
schema option to a function. Mongoose will
execute that function and use the return value as the default.
[require:Default functions]
setDefaultsOnInsert
OptionMongoose also sets defaults on update()
and findOneAndUpdate()
when the upsert
option is set by adding your schema's defaults to a MongoDB $setOnInsert
operator.
You can disable this behavior by setting the setDefaultsOnInsert
option to false
.
[require:The `setDefaultsOnInsert` option]
You can also set setDefaultsOnInsert
to false
globally:
mongoose.set('setDefaultsOnInsert', false);
this
Unless it is running on a query with setDefaultsOnInsert
, a default
function's this
refers to the document.
[require:Default functions and `this`]
Version Info