Do I have to rebuild MongoDB index when I add documents
Asked Answered
F

2

13

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)

Fletcher answered 13/3, 2018 at 0:32 Comment(0)
S
14

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.

Swiftlet answered 13/3, 2018 at 0:47 Comment(0)
C
9

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

Carbonize answered 13/3, 2018 at 0:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.