I am testing MongoDB (server v 2.6.7) with the C# driver 2.0.
When I am using the insert function InsertOneAsync
for a document with an _id
which exists I am expecting an error like the one you get from the Mongo shell:
WriteResult({ "nInserted" : 0, "writeError" : { "code" : 11000, "errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: mydb.Commands.$_id_ dup key: { : 0.0 }" }})
But the problem is that the insert with the C# driver does not throw an exception and I can not find the WriteResult
for the insert.
When I look in the database it seems nothing have happened.
So my question is what to expect from InsertOneAsync
when inserting an existing _id
?
The code in Visual Studio:
IMongoCollection<BsonDocument> commandsCollection = db.GetCollection<BsonDocument>("Commands");
var bson = new BsonDocument
{
{"_id", i.Value},
{"label", i.Key}
};
commandsCollection.InsertOneAsync(bson);