[CSHARP-4704] SetOnInsert not working as described Created: 01/Jul/23  Updated: 27/Oct/23  Resolved: 03/Jul/23

Status: Closed
Project: C# Driver
Component/s: Operations
Affects Version/s: 2.20.0
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Andreas Patock Assignee: Unassigned
Resolution: Gone away Votes: 0
Labels: Bug
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Documentation Changes Summary:

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



 Comments   
Comment by James Kovacs [ 03/Jul/23 ]

Thanks for letting us know.

Comment by Andreas Patock [ 02/Jul/23 ]

Please, close this issue. We have found the error. By misstake we use a development-mode-class which is calling "DropCollection" everytime. Sorry, for that.

Comment by PM Bot [ 01/Jul/23 ]

Hi apatock@bfe.tv, thank you for reporting this issue! The team will look into it and get back to you soon.

Generated at Wed Feb 07 21:49:05 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.