MongoDB C# driver 2.0 InsertManyAsync vs BulkWriteAsync

橙三吉。 提交于 2019-12-10 12:34:36

问题


I have to insert many documents in a MongoDB collection, using the new C# 2.0 driver. Is using either collection.InsertManyAsync(...) 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.


回答1:


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.



来源:https://stackoverflow.com/questions/32921533/mongodb-c-sharp-driver-2-0-insertmanyasync-vs-bulkwriteasync

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!