简体   繁体   English

MongoDB C#API的InsertMany未返回确认

[英]InsertMany from MongoDB C# API doesn't return acknowledged

As per documentation and code IMongoCollection.InsertMany method in MongoDB C# driver returns nothing. 根据文档和代码,MongoDB C#驱动程序中的IMongoCollection.InsertMany方法不返回任何内容。 It is strange because eg DeleteMany returns DeleteResult info. 这很奇怪,因为例如DeleteMany返回DeleteResult信息。 Even InsertMany as Shell method returns info with Acknowledged etc. (see here ). 甚至将InsertMany作为Shell方法都返回带有Acknowledged等的信息(请参见此处 )。

What is the reason of it? 是什么原因呢? And most important question: how to quickly (without sacrificing performance) get confirmation that InsertMany was correctly done ( Acknowledged == true )? 最重要的问题:如何快速(不牺牲性能)确认InsertMany已正确完成(已Acknowledged == true )?

Cause that's mostly not needed since if the record has been inserted then it will generate and populate the Id value to your object which has been passed for insertion. 原因是最不需要的,因为如果插入了记录,那么它将生成ID值并将其填充到传递给插入的对象中。 To see what I mean see this answer MongoDb bulk operation get id 要了解我的意思,请参见此答案MongoDb批量操作获取ID

Again, you can use the async version of this method with new driver InsertManyAsync() which returns a Task and as usual you can then check the Task.IsCompleted property to know whether it has completed successfully or not. 同样,您可以将此方法的异步版本与新驱动程序InsertManyAsync()结合使用,该驱动程序返回一个Task然后像往常一样,您可以检查Task.IsCompleted属性以了解其是否成功完成。

If you look at the API documentation ( http://mongodb.github.io/mongo-csharp-driver/2.7/apidocs/html/M_MongoDB_Driver_IMongoCollection_1_InsertMany_1.htm ) it seems that the InsertMany is a void method, so it returns nothing. 如果您查看API文档( http://mongodb.github.io/mongo-csharp-driver/2.7/apidocs/html/M_MongoDB_Driver_IMongoCollection_1_InsertMany_1.htm ),则InsertMany似乎是一个void方法,因此它不返回任何内容。

It's semantic it's exception based: if it completes without errors, so you are sure that all your documents have been inserted successfully 它的语义是基于异常的:如果完成没有错误,那么您可以确保所有文档都已成功插入

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM