-
Type:
Bug
-
Resolution: Gone away
-
Priority:
Major - P3
-
None
-
Affects Version/s: 2.20.0
-
Component/s: Operations
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);}}