Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB C# driver 2.0 InsertManyAsync vs BulkWriteAsync

I have to insert many documents in a MongoDB collection, using the new C# 2.0 driver. Is using either collection.InsertManyAsync(...) or collection.BulkWriteAsync(...) making any difference? (particularly about performance).

From what I understand from MongoDB documentation, an insert with an array of documents should be a bulk operation under the hood. Is that correct?

Thanks for your help.

like image 934
Stefano Castriotta Avatar asked Oct 03 '15 10:10

Stefano Castriotta


People also ask

Does MongoDB use C++?

Welcome to the documentation site for the official MongoDB C++ driver. You can add the driver to your application to work with MongoDB using the C++11 or later standard.

Can you use MongoDB with C#?

By developing with C# and MongoDB together one opens up a world of possibilities. Console, window, and web applications are all possible. As are cross-platform mobile applications using the Xamarin framework.

What is a MongoDB driver?

The official MongoDB Node. js driver allows Node. js applications to connect to MongoDB and work with data. The driver features an asynchronous API which allows you to interact with MongoDB using Promises or via traditional callbacks.


1 Answers

I found the answer looking at the driver source code: the InsertManyAsync uses internally the BulkWriteAsync.

So using InsertManyAsync it's the same as writing:

List<BsonDocument> documents = ...

collection.BulkWriteAsync(documents.Select(d => new InsertOneModel<BsonDocument>(d)));

Obviously, if all operations are Inserts, the InsertManyAsync should be used.

like image 141
Stefano Castriotta Avatar answered Sep 29 '22 14:09

Stefano Castriotta