Mongoose.prototype.set()
Parameters
- key «String»
- value «String|Function|Boolean»
Sets mongoose options
Example:
mongoose.set('test', value) // sets the 'test' option to `value`
mongoose.set('debug', true) // enable logging collection methods + arguments to the console/file
mongoose.set('debug', function(collectionName, methodName, ...methodArgs) {}); // use custom function to log collection methods + arguments
Currently supported options are
- ‘debug’: If
true
, prints the operations mongoose sends to MongoDB to the console. If a writable stream is passed, it will log to that stream, without colorization. If a callback function is passed, it will receive the collection name, the method name, then all arugments passed to the method. For example, if you wanted to replicate the default logging, you could output from the callbackMongoose: ${collectionName}.${methodName}(${methodArgs.join(', ')})
. - ‘returnOriginal’: If
false
, changes the defaultreturnOriginal
option tofindOneAndUpdate()
,findByIdAndUpdate
, andfindOneAndReplace()
to false. This is equivalent to setting thenew
option totrue
forfindOneAndX()
calls by default. Read ourfindOneAndUpdate()
tutorial for more information. - ‘bufferCommands’: enable/disable mongoose’s buffering mechanism for all connections and models
- ‘useCreateIndex’: false by default. Set to
true
to make Mongoose’s default index build usecreateIndex()
instead ofensureIndex()
to avoid deprecation warnings from the MongoDB driver. - ‘useFindAndModify’: true by default. Set to
false
to makefindOneAndUpdate()
andfindOneAndRemove()
use nativefindOneAndUpdate()
rather thanfindAndModify()
. - ‘useNewUrlParser’: false by default. Set to
true
to make all connections set theuseNewUrlParser
option by default - ‘useUnifiedTopology’: false by default. Set to
true
to make all connections set theuseUnifiedTopology
option by default - ‘cloneSchemas’: false by default. Set to
true
toclone()
all schemas before compiling into a model. - ‘applyPluginsToDiscriminators’: false by default. Set to true to apply global plugins to discriminator schemas. This typically isn’t necessary because plugins are applied to the base schema and discriminators copy all middleware, methods, statics, and properties from the base schema.
- ‘applyPluginsToChildSchemas’: true by default. Set to false to skip applying global plugins to child schemas
- ‘objectIdGetter’: true by default. Mongoose adds a getter to MongoDB ObjectId’s called
_id
that returnsthis
for convenience with populate. Set this to false to remove the getter. - ‘runValidators’: false by default. Set to true to enable update validators for all validators by default.
- ‘toObject’:
{ transform: true, flattenDecimals: true }
by default. Overwrites default objects totoObject()
- ‘toJSON’:
{ transform: true, flattenDecimals: true }
by default. Overwrites default objects totoJSON()
, for determining how Mongoose documents get serialized byJSON.stringify()
- ‘strict’: true by default, may be
false
,true
, or'throw'
. Sets the default strict mode for schemas. - ‘strictQuery’: false by default, may be
false
,true
, or'throw'
. Sets the default strictQuery mode for schemas. - ‘selectPopulatedPaths’: true by default. Set to false to opt out of Mongoose adding all fields that you
populate()
to yourselect()
. The schema-level optionselectPopulatedPaths
overwrites this one. - ‘typePojoToMixed’: true by default, may be
false
ortrue
. Sets the default typePojoToMixed for schemas. - ‘maxTimeMS’: If set, attaches maxTimeMS to every query
- ‘autoIndex’: true by default. Set to false to disable automatic index creation for all models associated with this Mongoose instance.
- ‘autoCreate’: Set to
true
to make Mongoose callModel.createCollection()
automatically when you create a model withmongoose.model()
orconn.model()
. This is useful for testing transactions, change streams, and other features that require the collection to exist. - ‘overwriteModels’: Set to
true
to default to overwriting models with the same name when callingmongoose.model()
, as opposed to throwing anOverwriteModelError
.
当前内容版权归 mongoosejs 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 mongoosejs .