Model.init()

Parameters
  • [callback] «Function»

This function is responsible for building indexes, unless autoIndex is turned off.

Mongoose calls this function automatically when a model is created using mongoose.model() or connection.model(), so you don’t need to call it. This function is also idempotent, so you may call it to get back a promise that will resolve when your indexes are finished building as an alternative to MyModel.on('index')

Example:

  1. const eventSchema = new Schema({ thing: { type: 'string', unique: true }})
  2. // This calls `Event.init()` implicitly, so you don't need to call
  3. // `Event.init()` on your own.
  4. const Event = mongoose.model('Event', eventSchema);
  5. Event.init().then(function(Event) {
  6. // You can also use `Event.on('index')` if you prefer event emitters
  7. // over promises.
  8. console.log('Indexes are done building!');
  9. });