Skip to content

Commit

Permalink
fix(index): add useCreateIndex option to avoid deprecation warnings
Browse files Browse the repository at this point in the history
Re: #6880
  • Loading branch information
vkarpov15 committed Aug 20, 2018
1 parent 1a661b1 commit 7ecce58
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
1 change: 1 addition & 0 deletions lib/index.js
Expand Up @@ -86,6 +86,7 @@ Mongoose.prototype.STATES = STATES;
* Currently supported options are:
* - 'debug': prints the operations mongoose sends to MongoDB to the console
* - '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()`.
* - '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.
Expand Down
10 changes: 6 additions & 4 deletions lib/model.js
Expand Up @@ -983,7 +983,11 @@ Model.init = function init(callback) {
const autoIndex = this.schema.options.autoIndex;
this.$init = new Promise((resolve, reject) => {
if (autoIndex || (autoIndex == null && this.db.config.autoIndex)) {
this.ensureIndexes({ _automatic: true }, function(error) {
const options = {
_automatic: true,
createIndex: this.base.options.useCreateIndex
};
this.ensureIndexes(options, function(error) {
if (error) {
return reject(error);
}
Expand Down Expand Up @@ -1185,9 +1189,7 @@ Model.ensureIndexes = function ensureIndexes(options, callback) {

/**
* Similar to `ensureIndexes()`, except for it uses the [`createIndex`](http://mongodb.github.io/node-mongodb-native/2.2/api/Collection.html#createIndex)
* function. The `ensureIndex()` function checks to see if an index with that
* name already exists, and, if not, does not attempt to create the index.
* `createIndex()` bypasses this check.
* function.
*
* @param {Object} [options] internal options
* @param {Function} [cb] optional callback
Expand Down

0 comments on commit 7ecce58

Please sign in to comment.