Model.createCollection()

Parameters

Create the collection for this model. By default, if no indexes are specified, mongoose will not create the collection for the model until any documents are created. Use this method to create the collection explicitly.

Note 1: You may need to call this before starting a transaction See https://docs.mongodb.com/manual/core/transactions/#transactions-and-operations

Note 2: You don’t have to call this if your schema contains index or unique field. In that case, just use Model.init()

Example:

  1. const userSchema = new Schema({ name: String })
  2. const User = mongoose.model('User', userSchema);
  3. User.createCollection().then(function(collection) {
  4. console.log('Collection is created!');
  5. });