Mongoose.prototype.set()

Parameters
  • key «String»
  • value «String|Function|Boolean»

Sets mongoose options

Example:

  1. mongoose.set('test', value) // sets the 'test' option to `value`
  2. mongoose.set('debug', true) // enable logging collection methods + arguments to the console/file
  3. 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 callback Mongoose: ${collectionName}.${methodName}(${methodArgs.join(', ')}).
  • ‘returnOriginal’: If false, changes the default returnOriginal option to findOneAndUpdate(), findByIdAndUpdate, and findOneAndReplace() to false. This is equivalent to setting the new option to true for findOneAndX() calls by default. Read our findOneAndUpdate() 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 use createIndex() instead of ensureIndex() to avoid deprecation warnings from the MongoDB driver.
  • ‘useFindAndModify’: true by default. Set to false to make findOneAndUpdate() and findOneAndRemove() use native findOneAndUpdate() rather than findAndModify().
  • ‘useNewUrlParser’: false by default. Set to true to make all connections set the useNewUrlParser option by default
  • ‘useUnifiedTopology’: false by default. Set to true to make all connections set the useUnifiedTopology option by default
  • ‘cloneSchemas’: false by default. Set to true to clone() 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 returns this 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 to toObject()
  • ‘toJSON’: { transform: true, flattenDecimals: true } by default. Overwrites default objects to toJSON(), for determining how Mongoose documents get serialized by JSON.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 your select(). The schema-level option selectPopulatedPaths overwrites this one.
  • ‘typePojoToMixed’: true by default, may be false or true. 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 call Model.createCollection() automatically when you create a model with mongoose.model() or conn.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 calling mongoose.model(), as opposed to throwing an OverwriteModelError.