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

SetOnInsert not working as described

    XMLWordPrintableJSON

Details

    • Icon: Bug Bug
    • Resolution: Gone away
    • Icon: Major - P3 Major - P3
    • None
    • 2.20.0
    • Operations
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

    Description

      Summary

      The setOnInsert updatedefinition is not working as described
      I wonder how to only insert a new Guid when the object was created and not updated.I thought therefore using the SetOnInsert is the right way - but it doesn't work!Every time I run the code below the object is getting a new _id:

      FilterDefinition<TestClass> filter = """{ Path: "a.b.c.d" }""";
      var builder = Builders<TestClass>.Update;
      var update = builder.SetOnInsert("_id", Guid.NewGuid()).Set("Path", "a.b.c.d").Set("Name", "c.d");
      FindOneAndUpdateOptions<TestClass> options = new FindOneAndUpdateOptions<TestClass> { IsUpsert = true, ReturnDocument = MongoDB.Driver.ReturnDocument.After };

      var read = collNamespace.FindOneAndUpdate<TestClass>(filter, update, options);
      How can I use FindOneAndUpdate or some other method to only set the _id once?

      {{FilterDefinition<TestClass> filter = """{ Path: "a.b.c.d" }""";

      var builder = Builders<TestClass>.Update;
      var update = builder.SetOnInsert("_id", Guid.NewGuid()).Set("Path", "a.b.c.d").Set("Name", "c.d");
      FindOneAndUpdateOptions<TestClass> options = new FindOneAndUpdateOptions<TestClass> { IsUpsert = true, ReturnDocument = MongoDB.Driver.ReturnDocument.After };

      var read = collNamespace.FindOneAndUpdate<TestClass>(filter, update, options);}}

      Attachments

        Activity

          People

            Unassigned Unassigned
            apatock@bfe.tv Andreas Patock
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: