Hi, @Jonny_Powell,
Welcome to the MongoDB Community Forums. I understand that you’re having some challenges mapping polymorphic types using the .NET/C# driver.
The _t field is used by the .NET/C# driver’s discriminator conventions when mapping polymorphic types to a collection. See Polymorphism in our driver documentation for more information on discriminator conventions.
Investigating your problem, I found a previously unknown bug, which I’ve filed as CSHARP-4040. When creating a mapping, we verify that your mapping does not contain any duplicate field names. However we do not include the discriminator conventions (which use _t by default) in this check. When you insert documents using this mapping into the collection, the BSON contains two fields named _t - one that you mapped and one from the discriminator convention.
Although BSON permits duplicate field names, their behaviour is undefined. The legacy mongo shell will only display the first duplicate. Compass and mongosh will only display the second duplicate. In C#, loading it as a BsonDocument will throw because of the duplicate field names, but deserializing it into a XDocument will read the second _t value into the field. How a particular driver handles duplicate field names in BSON is driver-dependent.
We recommend avoiding duplicate field names and in most cases our drivers prevent you from creating documents with duplicate field names. You’ve encountered an edge case where the .NET/C# driver does not include the discriminator field name (by default _t) in this check. We recommend avoiding the use of the field name _t in your mappings with the .NET/C# driver due to its use by our discriminator conventions.
Please let us know if you have any additional questions and thank you for helping us identify CSHARP-4040.
Sincerely,
James