Mongoose indexing in production code

后端 未结 3 1067
长发绾君心
长发绾君心 2020-11-29 16:03

Per the Mongoose documentation for MongooseJS and MongoDB/Node.js :

When your application starts up, Mongoose

3条回答
  •  情深已故
    2020-11-29 16:51

    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.

提交回复
热议问题