0

As per documentation and code IMongoCollection.InsertMany method in MongoDB C# driver returns nothing. It is strange because e.g. DeleteMany returns DeleteResult info. Even InsertMany as Shell method returns info with Acknowledged etc. (see here).

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

psur
  • 4,400
  • 26
  • 36

2 Answers2

1

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. To see what I mean see this answer MongoDb bulk operation get 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.

Rahul
  • 76,197
  • 13
  • 71
  • 125
1

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.

It's semantic it's exception based: if it completes without errors, so you are sure that all your documents have been inserted successfully

Enrico Massone
  • 6,464
  • 1
  • 28
  • 56