Mongoose Indexing Mongoose Indexing mongoose mongoose

Mongoose Indexing


The _id field is created automatically for every document created. You can not change its name or remove it.

employeesSchema will has 2 primary keys:

  • employee._id
  • employee.employeeId

that is why you see two index in your mongoose


Edit:

After create model, we must wait for index event before create document

User = mongoose.model('users', UserSchema);User.on('index', function () {   new User({}).save();   new User({}).save();   new User({}).save();   new User({}).save();})

If you do not wait for index event, the index may not be created (and you will not see any warning from mongoose about it)

I have report this to mongoose and get reply on this issue:https://github.com/LearnBoost/mongoose/issues/1745