I am using MongoDB for keeping log data. And my goal is zero dropped log record. Now I am using InsertManyAsync
for writing multiple log data. But in MongoDB there is also method like BulkWriteAsync
.
What is the difference in performance between InsertMany
and BulkWrite
? In local writing and writing over network?
With the insert_many , the driver is actually sending batches of docs (like 10k docs or more) instead of sending them one by one. It's a LOT more efficient but, depending on the options you set, you might fail and stop the processing on an error or not.
With insertOne you can insert one document into the collection. insertMany accepts an array of documents and these are inserted. The insert (the method from older versions) takes a single document by default, and there is an option to insert multiple documents supplied as an array.
The MongoDB\Driver\BulkWrite collects one or more write operations that should be sent to the server. After adding any number of insert, update, and delete operations, the collection may be executed via MongoDB\Driver\Manager::executeBulkWrite(). Write operations may either be ordered (default) or unordered.
insertMany() The insertMany() method inserts one or more documents in the collection. It takes array of documents to insert in the collection. By default, documents are inserted in the given order if you want to insert documents in unordered, then set the value of ordered to false.
Ok that's two questions:
InsertMany vs BulkWrite
Using BulkWrite
you can do many operations in a single connection to mongoDB. Internally, InsertMany
uses BulkWrite
, so there's no difference, it's just for convenience.
This question was already solved.
Sync vs Async
When you perform a sync operation, your aplication will wait for MongoDB to finalize the work. With a async operation you can perform many operations at the same time. Server, and client side.
This was already solved too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With