Mongoose indexing in production code

mongoose schema methods
mongoose virtual
mongoose unique index
mongoose drop index
mongoose create index if not exists
mongoose date
mongoose db
mongoose enum

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

When your application starts up, Mongoose automatically calls ensureIndex for each defined index in your schema. 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.

This appears to instruct removal of auto-indexing from mongoose prior to deploying to optimize Mongoose from instructing Mongo to go and churn through all indexes on application startup, which seems to make sense.

What is the proper way to handle indexing in production code? Maybe an external script should generate indexes? Or maybe ensureIndex is unnecessary if a single application is the sole reader/writer to a collection because it will continue an index every time a DB write occurs?

Edit: To supplement, MongoDB provides good documentation for the how to do indexing, but not why or when explicit indexing directives should be done. It seems to me that indexes should be kept up to date by writer applications automatically on collections with existing indexes and that ensureIndex is really more of a one-time thing (done when a new index is being applied), in which case Mongoose's autoIndex should be a no-op under a normal server restart.

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.

Mongoose indexing in production code - node.js - html, MongoDB indexes, like relational database indexes, are a construct for but that would not drop the index from your production database. Per the Mongoose documentation for MongooseJS and MongoDB/Node.js: When your application starts up, Mongoose automatically calls ensureIndex for each defined index in your schema. While nice for development, it is recommended this behavior be disabled in production since index creation can cause a significant performance impact.

Although I agree with the accepted answer, its worth noting that according to the MongoDB manual, this isn't the recommended way of adding indexes on a production server:

If your application includes ensureIndex() operations, and an index doesn’t exist for other operational concerns, building the index can have a severe impact on the performance of the database.

To avoid performance issues, make sure that your application checks for the indexes at start up using the getIndexes() method or the equivalent method for your driver and terminates if the proper indexes do not exist. Always build indexes in production instances using separate application code, during designated maintenance windows.

Of course, it really depends on how your application is structured and deployed. If you are deploying to Heroku, for example, and you aren't using Heroku's preboot feature, then it is likely your application is not serving requests at all during startup, and so it's probably safe to create an index at that time.

In addition to this, from the accepted answer:

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.

If you've managed to get your data model and queries nailed on first time around, this is fine, and often the case. However, if you are adding new functionality to your app, with a new DB query on a property without an index, you'll often find yourself adding an index to a collection containing many existing documents.

This is the time when you need to be careful about adding indexes, and carefully consider the performance implications of doing so. For example, you could create the index in the background:

db.ensureIndex({ name: 1 }, { background: true });

What's New in Mongoose 5.2.0: syncIndexes(), MongoDB supports secondary indexes. In Mongoose, we define these indexes within our schema. Defining indexes at schema level is necessary when we need​  This is called the Compount Index in mongodb. So it creates indexes as field1 and field1 + field2. So it is first index according to field1 and then inside field1 with respect to field 2 – Ketan Ghumatkar Jul 24 '15 at 15:46

use this block code to handle production mode:

const autoIndex = process.env.NODE_ENV !== 'production';
mongoose.connect('mongodb://localhost/collection', { autoIndex });

Mongoose v5.9.16: Schemas, If autoIndex is false, mongoose will not remove existing indexes, so any in production code for some queries, and through the debug setting,  Mongoose doesn't create TTL indexes. Ask Question Asked 5 years, 6 months ago. Active 5 years, 6 months ago. Mongoose indexing in production code. 111.

Node.js, syncIndexes() which triggers Mongoose to do a diff of indexes, and forcibly update them (ie; drop them and recreate them). This is necessary for production  The rest of this tutorial shows how to define and access the Mongoose schema and models for the LocalLibrary website example.. Designing the LocalLibrary models. Before you jump in and start coding the models, it's worth taking a few minutes to think about what data we need to store and the relationships between the different objects.

autoIndex doesn't work for `mongoose.connect()` · Issue #5176 , Drivers API · Production Considerations · Production Considerations (Sharded Clusters) Index Intersection · Manage Indexes · Measure Index Use · Indexing MongoDB Wire Protocol · Log Messages · Exit Codes and Statuses · Glossary  This is great for development, but not ideal for large production deployments, because index builds can cause performance degradation. If you set autoIndex to false, mongoose will not automatically build indexes for any model associated with this connection.

Have Mongoose sync indexes with Mongo on boot · Issue #431 , MongoDB allows you to create indexes on other properties in your Schema A common mistake I see in NodeJS code whenever people use  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 .

Comments
  • I have a question to add...What if I set it false? Than will the indexes be created when i insert the data or do I need to explicitly create it. I am sorry if this is a novice question but it would be really helpful if you answered.
  • @SaranshMohapatra When autoIndex is false, you need to call ensureIndexes on your model to create its indexes.
  • Than will I have to call it every time or just once defining the model?
  • @SaranshMohapatra when you define (compile) your model. I do that when I first start the app. Now the hard think is to decide to drop all indexes and recreate them, in case you schema changes.
  • @JohnnyHK do you still agree with your answer now that it's almost 2016?
  • Ok, so all you have to do is NOT start your server until all the ensureIndex callbacks have fired for each collection.
  • @AlexMills how do you ensure that?
  • async.each(Object.keys(models), function (key, cb) { models[key].ensureIndexes(cb) }, cb)
  • just call ensureIndexes on each mongoose model, wait for all to finish, then start your server; I also recommend waiting for db connections to happen before starting your server as well
  • There is no ensureIndex anymore. There is createIndex instead. Am I right?