我正在try 使用.网络驱动程序.它似乎不会自动生成_id
-on-upserts,尽管它确实正确地生成了_id
-on-plain insert.如何使驱动程序正确生成_id
?
下面是一个小例子来说明这个问题.
public class MongoObject
{
[BsonId(IdGenerator = typeof(StringObjectIdGenerator))]
[BsonRepresentation(BsonType.ObjectId)]
public string MongoID { get; set; }
public int Index { get; set; }
}
var obj = new MongoObject()
{
Index = 1
};
//This inserts the document, but with a _id set to Null
_collection.Update(Query.EQ("Index", BsonValue.Create(1)), Update.Replace(obj), UpdateFlags.Upsert, new WriteConcern() { Journal = true });
//This inserts the document with the expected autogenerated _id
//_collection.Insert(obj);