0

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.

  1. Why do Mongoose has to worry about index creation? Isn't that part of MongoDb?
  2. 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?
  3. 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?
  4. 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.

Kalesh Kaladharan
  • 1,038
  • 1
  • 8
  • 26
  • 2
    1. Indexes are part of the database, the API just tries to create them every time the application is started ( by default ). 2. No the API **does not** *keep track of schema changes*. Build your own tools or search the web for that if you must. 3. a **`createIndex()`** ( older answers might say `ensureIndex()`, but this has been deprecated and replaced ) does not actually do anything where the index already exists ( covered in linked answer ) 4. This was really the same question. Mongoose does not *need* to do anything here It's all optional. – Neil Lunn Oct 29 '19 at 09:33
  • @NeilLunn Thank you. With the 3rd answer you have given, `autoIndex:true` will not cause any issue to my code. Should I keep this thread or delete it? – Kalesh Kaladharan Oct 29 '19 at 09:56
  • 1
    `autoIndex: true` is the default action of Mongoose. If you want different behavior then you would change that to `false`, or not. Despite the sentiment of the accepted answer in the link, I disagree and think this has no place in production systems as any "schema management" really should be handled by specific deployment scripting. IMHO, (but humble based on pretty large deployments ). – Neil Lunn Oct 29 '19 at 10:00
  • 1
    I really don't understand the sentiment of "should I delete my post". *"Could you have done some more research before posting?"*, probably Yes. But I really see no general shame or harm in similar/same questions being asked as they generally probably help by using *slightly* different terms to the original that others might also think of and search for, and thus find the *canonical* source. Questions that are almost identical in title and question structure however are another matter. Do whatever you want. – Neil Lunn Oct 29 '19 at 10:03

0 Answers0