I just started learning MongoDb and am using Mongoose in a project. Coming from a RDBMS background, I know indexing is really important when we are dealing with millions of data. I went through the Mongoose documentation and it is written that
When your application starts up, Mongoose automatically calls createIndex for each defined index in your schema. Mongoose will call createIndex for each index sequentially, and emit an 'index' event on the model when all the createIndex calls succeeded or when there was an error. While nice for development, it is recommended this behavior be disabled in production since index creation can cause a significant performance impact. Disable the behavior by setting the autoIndex option of your schema to false, or globally on the connection by setting the option autoIndex to false.
Given below are the questions that I have. And these are for a populated database that already have indexes.
- Why do Mongoose has to worry about index creation? Isn't that part of MongoDb?
- If Mongoose could keep track of the schema changes and database indexes, will it be able to avoid this index creation on restarts if there is no schema change?
- In production, we add new features very often and will Mongoose go through the index creation process each time we restart the app? How fast will this be if we already have index on 100K documents in a collection?
- What does MongoDb do when a create index is requested for an already existing index field? Will it quickly return a success response or perform an update on the index?
Thank you.