Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I have to rebuild MongoDB index when I add documents

I have just discovered the extraordinary power of mongodb indexes. Building indexes on just a few fields has allowed me to speed up some operations 1000 fold, and more. My apologies for what might seem a stupid question: once I have built an index on particular fields in my database, will that rebuild itself automatically every time I add and remove documents? Or do I have to call it explicitly? (in a development phase, I might remove EVERY document, and then add them all again)

like image 662
Peter Robinson Avatar asked Mar 13 '18 00:03

Peter Robinson


People also ask

How do I update an existing index in MongoDB?

To modify an existing index in the MongoDB Shell, you need to drop and recreate the index. The exception to this rule is TTL indexes, which can be modified via the collMod command in conjunction with the index collection flag.

How do I know when to rebuild my index?

There are two rules of thumb to help determine if the index needs to be rebuilt: If the index has height greater than four, rebuild the index. The deleted leaf rows should be less than 20%.

Does MongoDB automatically index?

MongoDB automatically determines whether to create a multikey index if the indexed field contains an array value; you do not need to explicitly specify the multikey type.

How are indexes maintained in MongoDB?

Indexes in MongoDB Without them, the database must scan every document in a collection or table to select those that match the query statement. If an appropriate index exists for a query, the database can use the index to limit the number of documents it must inspect.

How to rebuild indexes in MongoDB?

Rebuilding indexes in MongoDB? Rebuilding indexes in MongoDB? To rebuild indexes, use reIndex (). Let us first create an index. Following is the query −

What is a compound Index in MongoDB?

MongoDB indexes can be created and dropped on-demand to accommodate evolving application requirements and query patterns and can be declared on any field within your documents, including fields nested within arrays. So let's cover how you make the best use of indexes in MongoDB. Compound indexes are indexes composed of several different fields.

When should I reindex my MongoDB indexes?

According to MongoDB documentation: Normally, MongoDB compacts indexes during routine updates. For most users, the reIndex command is unnecessary. However, it may be worth running if the collection size has changed significantly or if the indexes are consuming a disproportionate amount of disk space.

How do I view all indexes in MongoDB Compass?

To view a list of all indexes on a collection in MongoDB Compass , click on the target collection in the left-hand pane and select the Indexes tab. For details on the information displayed in this tab, refer to the Compass documentation.


2 Answers

Once the index is created, it is automatically maintained by Mongo. No need to rebuild it manually.

like image 134
see sharper Avatar answered Sep 19 '22 09:09

see sharper


MongoDB automatically keeps indexes up-to-date for you, reflecting the current status of the documents in the collections. You do not have to worry about indexes update!

A little caveat about indexes. Quoting the doc:

Although indexes can improve query performances, indexes also present some operational considerations. See Operational Considerations for Indexes for more information.

For example, if you need to do a bulk insert hitting an index and you don't want to slow down that process, sometimes it could be useful deleting the index, then inserting the huge collection of documents and finally rebuild the index from scratch (also in background, if necessary). Further info and other considerations in the link above.

like image 32
floatingpurr Avatar answered Sep 19 '22 09:09

floatingpurr