2

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

3
  • I haven't used C# in awhile but it looks like they introduced an await operator like JavaScript. My guess is that's your issue. Commented Apr 15, 2020 at 23:38
  • 1
    @Chance I believe C# had await before it was introduced in JS :) Commented Apr 16, 2020 at 0:08
  • @mickl interesting, I had no idea. I really need to look into C# again. I moved on to Rails and then Node when it was released. I think I left .Net behind sometime around 3.5/4.0. I remember really missing the lambdas that were introduced with LINQ but JS eventually got them. Commented Apr 16, 2020 at 1:04

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.