[CSHARP-2415] Different behavior between Upsert and Insert with default ObjectId Created: 11/Oct/18 Updated: 27/Oct/23 Resolved: 11/Oct/18 |
|
| Status: | Closed |
| Project: | C# Driver |
| Component/s: | API |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Task | Priority: | Major - P3 |
| Reporter: | Sun Yu | Assignee: | Robert Stam |
| Resolution: | Works as Designed | Votes: | 0 |
| Labels: | question | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Description |
|
I found the behavior between Insert and Upsert is differenct when I use C# Driver v2.7. Something like this: An entity such as:
When I use Insert Method:
it's correctly stored in DB with generated ObjectId like this:
But when I use Upsert such as:
it's stored with default ObjectId (all zero) like:
Is it the reasonable behavior difference between Upsert and Insert ? |
| Comments |
| Comment by Robert Stam [ 11/Oct/18 ] | ||||||
|
This is expected behavior. The difference is that in the InsertOne case it is the driver that is responsible for generating the ObjectId, whereas in the ReplaceOne (with upsert) case it is the server that is responsible for generating the ObjectId if necessary. However:
In your ReplaceOnce example the entity value you are sending to the server is not missing, it is all zeros so the server does not generate the _id value because one is already present (and has a value of all zeros). If you actually want ReplaceOne to generate the _id server side you should configure your POCO so that when the _id value is all zeros it is not serialized.
|