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
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; }
}