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
-
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.Chance– Chance2020-04-15 23:38:03 +00:00Commented Apr 15, 2020 at 23:38
-
1@Chance I believe C# had await before it was introduced in JS :)mickl– mickl2020-04-16 00:08:01 +00:00Commented 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.Chance– Chance2020-04-16 01:04:26 +00:00Commented Apr 16, 2020 at 1:04
Add a comment
|
1 Answer
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.