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:
const schema = new Schema({ name: { type: String, unique: true } });
const Customer = mongoose.model('Customer', schema);
await Customer.collection.createIndex({ age: 1 }); // Index is not in schema
// Will drop the 'age' index and create an index on `name`
await Customer.syncIndexes();
当前内容版权归 mongoosejs 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 mongoosejs .