Do I have to rebuild MongoDB index when I add documents

喜你入骨 提交于 2019-12-23 10:27:10

问题


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)


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/49246495/do-i-have-to-rebuild-mongodb-index-when-i-add-documents

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!