Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-1545

IdGenerator possibly not honoured on ReplaceOne upsert? (Works on InsertOne)

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • None
    • 2.2
    • None
    • None

    Description

      Hopefully I'm not misunderstanding something simple, but I have noticed I get different _id values generated in the database depending on whether I use InsertOne or ReplaceOne.
      I try to avoid any dependancy on MongoDB libraries etc, so the Id field in my objects is always a string. However, I want to use the ObjectId generation pattern. In much older versions of the driver, I was using the StringObjectIdGenerator, and am trying to do so now too.
      I'm using custom conventions to do my mapping (nothing strange in it, just that I use my own attributes to identify, for example, the ID field), and the important code looks like this:
      var cmm = memberMap.ClassMap;
      cmm.MapIdMember(memberMap.MemberInfo)
      .SetSerializer(new StringSerializer(BsonType.ObjectId).WithRepresentation(BsonType.String))
      .SetIgnoreIfDefault(true)
      .SetIdGenerator(StringObjectIdGenerator.Instance);

      If I call InsertOne, I get this:
      "_id" : "56a75e69e73e572dbc1b75c4"

      and if I call ReplaceOne (with a new document/no id set) I get this:
      "_id" : ObjectId("56a75e6a87dc53ded31754e8")

      Attachments

        Activity

          People

            Unassigned Unassigned
            kevinza Kevin Versfeld
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: