[CSHARP-2389] Find fails when a result document has duplicate element names Created: 17/Sep/18  Updated: 31/Mar/22

Status: Backlog
Project: C# Driver
Component/s: Read Operations
Affects Version/s: 2.7.0
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Adam Milazzo Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

All


Attachments: File CSHARP-2389.url    
Backwards Compatibility: Fully Compatible

 Description   

CursorBatchDeserializationHelper.DeserializeBatch creates a deserialization context that does not support documents with duplicate element names. This prevents the Find() method from being used to query collections with such documents. A fix is to change this line:

var context = BsonDeserializationContext.CreateRoot(reader);

 

to this line:

 

var context = BsonDeserializationContext.CreateRoot(reader, b => b.AllowDuplicateElementNames = true);



 Comments   
Comment by Robert Stam [ 09/Oct/18 ]

Agreed that you can create BsonDocuments with duplicate element names by setting AllowDuplicateElementNames to true. Even so it is still not recommended. The behavior of documents with duplicate element names is not well defined in general.

Your use case of a general purpose tool being able to read any and all existing documents (even if they somehow ended up with duplicate element names) sounds valid.

We will look into how to support this. It's not clear that it should be the default.

Comment by Adam Milazzo [ 08/Oct/18 ]

They can be created by the C# driver. You just set AllowDuplicateElementNames to true.

The C# driver should be able to query any MongoDB server, not just servers whose contents were inserted by the C# driver with the default settings.

I find it useful because I'm working on a tool that can connect to any MongoDB server, and it shouldn't fail if a server happens to have documents with duplicate element names, which are supported even if not recommended.

Comment by Robert Stam [ 08/Oct/18 ]

Documents with duplicate element names are not recommended, and in fact can't normally be created using the C# driver in the first place.

Can you elaborate on how you ended up with a document that has duplicate element names and why you would find that useful?

Comment by Adam Milazzo [ 17/Sep/18 ]

There are many other places where deserialization contexts are created, and many of them may also cause problems. It's unclear which of them should enable this option, but I believe that they should be enabling that option unless there's a good reason not to.

Some other relatively obvious places: ChangeStreamCursor.cs and ParallelScanOperation.cs

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