Mongoose indexing in production code

I’ve never understood why the Mongoose documentation so broadly recommends disabling autoIndex in production. Once the index has been added, subsequent ensureIndex calls will simply see that the index already exists and then return. So it only has an effect on performance when you’re first creating the index, and at that time the collections are often empty so creating an index would be quick anyway.

My suggestion is to leave autoIndex enabled unless you have a specific situation where it’s giving you trouble; like if you want to add a new index to an existing collection that has millions of docs and you want more control over when it’s created.

Leave a Comment