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

Different behavior between Upsert and Insert with default ObjectId

    • Type: Icon: Task Task
    • Resolution: Works as Designed
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: API

      I found the behavior between Insert and Upsert is differenct when I use C# Driver v2.7. Something like this:

      An entity such as:

      // code placeholder
      public class Person
      {
          public ObjectID _id { get; set; }
          public string Name { get; set; }
      }
      

       

      When I use Insert Method:

      // code placeholder
      var collection = db.GetCollection<Person>(collectionName);
      Person entity = new Person();
      entity.Name = "Peter";
      
      collection.InsertOne(entity);
      

       

      it's correctly stored in DB with generated ObjectId like this:

      // code placeholder
      {
        "_id":"5bb67b8b8d3632037461b621",
        "Name":"Peter"
      }
      

       

      But when I use Upsert such as:

      // code placeholder
      ......
      
      collection.ReplaceOne(item => false, entity, new UpdateOptions() { IsUpsert = true });
      

       

      it's stored with default ObjectId (all zero) like:

      // code placeholder
      {
        "_id":"000000000000000000000000",
        "Name":"Peter"
      }
      

       

      Is it the reasonable behavior difference between Upsert and Insert ?

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            sunyutj Sun Yu
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: