[CSHARP-4027] BsonDiscriminator not always written Created: 31/Jan/22  Updated: 31/Mar/22

Status: Backlog
Project: C# Driver
Component/s: None
Affects Version/s: 2.14.1, 2.13.3
Fix Version/s: None

Type: Bug Priority: Unknown
Reporter: Marc Simkin Assignee: Robert Stam
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: Zip Archive MongoDiscriminatorTest.zip    

 Description   

Summary

When using FindOneAndUpdate(Async) with Upsert = true and only SetOnInsert fields, BsonDiscriminator is not written.

Please provide the version of the driver. If applicable, please provide the MongoDB server version and topology (standalone, replica set, or sharded cluster).

Driver 2.13.3 and 2.14.1, .NET 6

Sharded Clusters 3.4.10 and 3.4.16

Standalone: 5.0.5

How to Reproduce

See attached Zip.

  1. Create a Class.
  2. Create a ClassMap. ClassMap should set discriminator name, required, root, and add known type. (see file BasicLockBlock.cs in attached Zip.)
  3. Register ClassMap.
  4. Execute FindAndUpdate(Async) for a document that doesn't exist. Use Upsert = true to insert new document. (see file program.cs in attached Zip.)

Additional Background

Please provide any additional background information that may be helpful in diagnosing the bug.



 Comments   
Comment by Marc Simkin [ 24/Feb/22 ]

@Robert Stam, I have spent some time thinking about this issue.

IMO, if a NEW document is being inserted, via upsert, than the discriminator should be written.

If the document is being UPDATED, if the discriminator doesn't exist (or it has a different value) it should NOT be modified.

 

Comment by Marc Simkin [ 02/Feb/22 ]

Your welcome.

Thank you for the work around.

 

Comment by Robert Stam [ 02/Feb/22 ]

Thank you for reporting this. We're thinking about what the proper behavior should be. The way upsert normally works is that it creates a new document by combining values from the filter with instructions from the update specification. This process doesn't normally involve automatically modifying the update instructions to add an additional field like the discriminator. Though maybe it should.

An easy workaround in the meantime is to add the discriminator yourself:

.SetOnInsert("_t", "BasicLockBlock")

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