bulkWrite vs initialize(Un)orderedBulkOp
what is the differences between those 2 methods, and which should I use?
- what is the diff between: initializeUnorderedBulkOp and bulkWrite with ordered: false
- what is the diff between: initializeOrderedBulkOp and default bulkWrite
https://docs.mongodb.com/manual/reference/method/db.collection.initializeUnorderedBulkOp/
https://docs.mongodb.com/manual/reference/method/db.collection.initializeOrderedBulkOp/
https://docs.mongodb.com/manual/core/bulk-write-operations/
TL;DR
The difference is mainly in the usage. bulkWrite
takes in an array of operations and executes it immediately.
initializeOrderedBulkOp
and initializeUnorderedBulkOp
return an instance which can be used to build queries gradually and execute it at last using the execute
function.
Late to the party but I had a similar confusion so did some digging up.
The difference lies in the API implementation and usage.
-
bulkWrite
According to the API reference,
Perform a bulkWrite operation without a fluent API
In this method, you directly pass in an array of "write operations" as the first argument. See here for examples. I think by fluent API, they mean you don't exactly separate your
update
operations from yourinsert
operations ordelete
operations. Every operation is in one array.One crucial point is These operations are executed immediately.
As noted in the question, the execution is ordered by default but can be changed to unordered by setting
{ ordered: false }
in the second argument which is a set of options.The return value of the function is
BulkWriteResult
which contains information about the executed bulk operation. -
initializeOrderedBulkOp
andinitializeUnorderedBulkOp
Referring to the API reference again,
Initiate an In order bulk write operation
As it says here, these methods initialize/return an instance which provides an API for building block operations. Those instances are of the class
OrderedBulkOperation
andUnorderedBulkOperation
respectively.const bulk = db.items.initializeUnorderedBulkOp(); // `bulk` is of the type UnorderedBulkOperation
This
bulk
variable provides a "fluent API" which allows you to build your queries across the application:bulk.find( { /** foo **/ } ).update( { $set: { /** bar **/ } } );
Bear in mind, these queries are not executed in the above code. You can keep on building the whole operation and when all the write operations are "called", we can finally execute the query:
bulk.execute();
This
execute
function returns aBulkWriteResult
instance which is basically whatbulkWrite
returns. Our database is finally changed.
Which one should you use?
It depends on your requirements.
If you want to update a lot of documents with separate queries and values from an existing array, bulkWrite
seems a good fit. If you want to build your bulk operation through a fairly complex business logic, the other options make sense. Note that you can achieve the same by constructing a global array gradually and passing it in the end to bulkWrite
.