How to index mongoose fields in production mode, why should i disable autoindex feature? How to index mongoose fields in production mode, why should i disable autoindex feature? mongoose mongoose

How to index mongoose fields in production mode, why should i disable autoindex feature?


Question 1: Why should I disable autoMigrate on production?

For many projects, production databases are very large compared to dev databases, so if you would migrate the database (adding new indexes etc) on application startup, you'd experience a lot of unexpected downtime.

It's best practice to do heavy production migrations outside of the application life cycle, either by using a migration framework or by manually creating indexes on your production instance directly.

Question 2: Why do I need to create indexes in mongoose?

You don't strictly need them - you could just leave them out of your schema definition and always create your indexes directly in mongodb.

But they give you some convenience: In dev environments where auto-migration is enabled, defining the index on your schema allows mongoose to create the indexes in your dev mongodb on your behalf.

And if you use migrate-mongoose as above, you can also use them for production migrations. That reduces repetition and room for human error.