Efficiently Update 1 million documents in the mongodb collection

223 Views Asked by At

I need to update around 1-1.5 million documents in a Mongodb collection.

Is updateMulti() an efficient way to update the document? I mean will it update the documents in batches internally?

    import org.springframework.data.mongodb.core.MongoOperations.updateMulti;

    mongoOperations.updateMulti(query, update, "your_collection_name");
1

There are 1 best solutions below

0
Soheil Babadi On

When you use updateMulti(), it sends a single update command to the MongoDB server, which will update all the matched documents in one operation. This can be more efficient than updating the documents one by one, as it reduces the network round-trips between the application and the database.

If you need to update a large number of documents (1-1.5 million in your case), I recommend to using bulk operations instead of updateMulti(). Bulk operations allow you to perform multiple write operations in batches, reducing the overhead of individual update commands.You can use CUD methods to perform different types of operations in a single DB call.