MongoTemplate BulkOperations atomicity


When I use bulk operations as in the example below (inserting a new document, and then updating it it with server-side date) - does the mongo-server creates a new document without the Date, and then updates it with Date, or the document will be created with Date from the beginning.

I want to know if there could be happen that a client will fetch the document without the Date?

    public void testInsertAndUpdate() {
        ObjectId clientSideObjectId = new ObjectId();
        TestedObjectWithId object = new TestedObjectWithId("name", clientSideObjectId);
        BulkOperations bulkOperations =
                mongoTemplate.bulkOps(UNORDERED, COLLECTION_NAME)
                                new Update().currentDate("serverSideCurrentDate")
1 Like

means that the operations are NOT ORDERED. It means that the operations are NOT guarantied to be executed in the order you specified. So potentially, updateOne is performed before the insert, so the query part will not matched any document because it is not inserted yet. The bulkWrite will then leave a document without the server side current date.

A transaction would be a better choice to make sure a date is set before anyone retrieve the document.

1 Like

and if I change it to ORDERED?

1 Like

Since it is not a

someone can retrieve the document after the insertion and before you have the time to set the date. Unlikely but possible. Always plan for the worst case.

1 Like


For my case maybe it is sufficient, since we are going to fetch according to the Date field, so it will be fetched only after the last update. I will investigate transaction also, and compare the performance of the insert+update with transaction.

Thanx again!


In MongoDB, bulk operations like insert and update are executed in order and atomically, meaning either all operations succeed or all operations fail.

In the example you provided, the MongoDB server will create the document with the “name” field and the client-side generated ObjectId, and then update the document to include the “serverSideCurrentDate” field.

There is no possibility for a client to fetch the document without the “serverSideCurrentDate” field, as the update operation is guaranteed to be atomic and successful.

Read about bulk operation. Your understanding about

seems to different from what is written in the documentation. In particular:

With an unordered list of operations, MongoDB can execute the operations in parallel, but this behaviour is not guaranteed. If an error occurs during the processing of one of the write operations, MongoDB will continue to process remaining write operations in the list.

You wrote

but the documentation says

execute the operations in parallel
So the order cannot be guarantee.

You write

but the documentation says:

If an error occurs during the processing of one of the write operations, MongoDB will continue to process remaining write operations in the list.

So the insert and its subsequent updateOne are not atomic to each other, one might fail but not the other.

So like I wrote

Thanks for the feedback I may have understood it in a different way @steevej .

I will keep you posted thanks alot.It feels really great and good when people like u are there to lift