Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB InsertMany vs BulkWrite

Tags:

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?

like image 860
Alisettar Huseynli Avatar asked Mar 02 '16 21:03

Alisettar Huseynli


People also ask

Why is insertMany () faster than insertOne ()?

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.

What is the difference between insert and insertMany in MongoDB?

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.

What is BulkWrite in MongoDB?

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.

What does insertMany return in MongoDB?

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.


1 Answers

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.

like image 133
Adrian Lopez Avatar answered Sep 24 '22 17:09

Adrian Lopez