MongoDB C#驱动程序2.0 InsertManyAsync与BulkWriteAsync [英] MongoDB C# driver 2.0 InsertManyAsync vs BulkWriteAsync

查看:639
本文介绍了MongoDB C#驱动程序2.0 InsertManyAsync与BulkWriteAsync的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用新的C#2.0驱动程序在MongoDB集合中插入许多文档.是否使用任一collection.InsertManyAsync(...)collection.BulkWriteAsync(...)有什么不同? (尤其是关于性能).

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

据我从MongoDB文档中了解,带有一系列文档的插入应该是在后台进行的批量操作.那是对的吗?

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?

感谢您的帮助.

推荐答案

我在查看驱动程序源代码时找到了答案:InsertManyAsync在内部使用BulkWriteAsync,因此使用InsertManyAsync与编写相同:

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)));

很显然,如果所有操作都是Inserts,则应使用InsertManyAsync.

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

这篇关于MongoDB C#驱动程序2.0 InsertManyAsync与BulkWriteAsync的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆