Rebuild index necessary?

Hi,

We are building a Java app to always replace documents in a large collection. Is rebuilding index necessary for MongoDB in this case? I read the document that db.collect.reIndex() is depricated in 6.0 and for most users, the db.collection.reIndex() command is unnecessary. Is this because MongoDB automatically updates indexes to make it perform efficiently without rebuild indexes?

Hi @Linda_Peng and welcome to MongoDB community forums!!]

The indexes in MongoDb are created on collection rather on the documents. Therefore, unless the collection is dropped the indexes would still remain for the collection.
Could you confirm, if your Java application replaces the documents in the collection by dropping the collection or rewrites in the same collection.

In the later case, you would have to create the indexes on the new collection being created from the MongoDB’s end.
You can make use of Atlas or Compass or even CLI to create indexes to the collection.

Please visit the documentation on Modify Indexes in MongoDB for further details.

Please reach out in case of any further questions.

Best Regards
Aasawari

The Java app could potentially replace ~100 million documents in the collection in the peak day. Collection is not dropped. The reason the java app is replacing instead of updating is due to some simplicity from application coding perspective (beyond my knowledge). Question is, if so many documents are replaced in a day, what would happen to the index performance? In relational database, you need to periodically rebuild index for index efficiency.

FYI:
There will be some performance impact on write operations from indexes, so depending on how many indexes you have, how many documents you have, and the frequency of insert you could see a bit of performance impact when writing and deleting (since this counts as a write operation). But you will not need to re-add your indexes.

From the MongoDB Docs v

Write Operation Performance

Indexes

Each index on a collection adds some amount of overhead to the performance of write operations.

For each insert or delete write operation on a collection, MongoDB either inserts or removes the corresponding document keys from each index in the target collection. An update operation may result in updates to a subset of indexes on the collection, depending on the keys affected by the update.

For indexes and queries, see Query Optimization. For more information on indexes, see Indexes and Indexing Strategies.

2 Likes