Model.syncIndexes()

Parameters
  • [options] «Object» options to pass to ensureIndexes()

  • [options.background=null] «Boolean» if specified, overrides each index’s background property

  • [callback] «Function» optional callback

Returns:
  • «Promise,undefined» Returns undefined if callback is specified, returns a promise if no callback.

Makes the indexes in MongoDB match the indexes defined in this model’s schema. This function will drop any indexes that are not defined in the model’s schema except the _id index, and build any indexes that are in your schema but not in MongoDB.

See the introductory blog post for more information.

Example:

  1. const schema = new Schema({ name: { type: String, unique: true } });
  2. const Customer = mongoose.model('Customer', schema);
  3. await Customer.collection.createIndex({ age: 1 }); // Index is not in schema
  4. // Will drop the 'age' index and create an index on `name`
  5. await Customer.syncIndexes();