InsertOne in .net core return void


I am new in MongoDB.

The InsertOne And InsertMany return null,

How can I be sure data were inserted ?

Hi @omid_mirzaei, welcome!

For write operations such as InsertOne and InsertMany, you need to catch for MongoWriteException. There are two properties of this exception:

  • WriteError - there was an error applying the write operation.
  • WriteConcernError - an error that indicates that the server was unable to guarantee the write operation to the level specified.

For more information please see: MongoDB .NET/C# driver: Write Exceptions and MongoDB .NET/C# driver WriteConcern.

I would also suggest to review Write Concern concept in the documentation to understand the concept of write acknowledgement.

I would recommend to enrol in MongoDB free online courses to learn more about MongoDB. Especially M220N: MongoDB for .NET developers to learn the essentials .NET application development. The next session starts soon on April 28th.


1 Like