omid mirzaei
omid mirzaei

Reputation: 53

InsertOne mongodb in .net core does not return result

i am new in mongodb And when i use InsertOne Or InsertMany methods does not return any result. how can i be sure data were inserted. I Use MongoDB.Driver Version 2.10.3 Sorry for my language

Upvotes: 2

Views: 2214

Answers (1)

mickl
mickl

Reputation: 49945

You can check the implementation here.

Basically whenever something goes wrong like there's a connection errror or your insert violates unique key you will get a MongoWriteException. So your code may look like this:

try
{
    collection.InsertOne(document);
}
catch (MongoWriteException e)
{
    Console.WriteLine(e.Message);
}

There's another method in MongoDB - bulkWrite - which might execute some operations successfully and fail for the other ones. In such case MongoDB .NET driver returns an instance of BulkWriteResult<TDocument> class.

Upvotes: 2

Related Questions