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.
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.
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.
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.
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.
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