Vidhyanshu jain
Vidhyanshu jain

Reputation: 341

How does the Bulkwrite operation in Mongodb work?

I wanted to do a bulkwrite on my MongoDb, the Collection on which I am using the bulkwrite is very simple.

What I am not sure about is weather it would be right for me to use bulkwrite. I have the following questions:

1) One if I do a bulkwrite operation is it atomic?

2) Will bulkwrite write do all the updates simultaniously or can there be delays between the writes?

    bulkArr.push({
            updateOne: {
                        "filter": { "_id": input[i].variantId },
                        "update": { $inc: { "stocks": -quant } }
                        }
                 });

Upvotes: 4

Views: 3145

Answers (1)

ambianBeing
ambianBeing

Reputation: 3529

bulkwrite operation, is it atomic?

From the docs

  • a write operation is atomic on the level of a single document, even if the operation modifies multiple embedded documents within a single document.
  • When a single write operation (e.g. updateMany() or bulkWrite()) modifies multiple documents, the modification of each document is atomic, but the operation as a whole is not atomic.

Will bulkwrite write do all the updates simultaneously?

  • Not really, The { ordered : false/true } parameter specifies whether bulkWrite() will execute operations in order or not. By default, operations are executed in order. docs

  • Also in bulkWrite() operations of the same type (e.g. multiple inserts) may be grouped together and sent to the server, Which happens in batch sizes not exceeding 100,000 writes.

Upvotes: 3

Related Questions