MongoDB .NET not generating _id on upsert

前端 未结 1 1401
无人共我
无人共我 2020-12-10 02:53

I\'m attempting to upsert a document into MongoDB 2.4.4 using the .NET driver. It seems not to automatically generate the _id on upserts, though it does correct

相关标签:
1条回答
  • 2020-12-10 03:42

    And of course I find the answer immediately after posting the question. From this answer, the solution is to add a [BsonIgnoreIfDefault] attribute to the ID. In the example from the question it would be:

    public class MongoObject
    {
        [BsonId(IdGenerator = typeof(StringObjectIdGenerator))]
        [BsonRepresentation(BsonType.ObjectId)]
        [BsonIgnoreIfDefault]    // <--- this is what was missing
        public string MongoID { get; set; }
    
        public int Index { get; set; }
    }
    
    0 讨论(0)
提交回复
热议问题