[CSHARP-1913] LINQ SelectMany operator on Dictionary Created: 09/Feb/17  Updated: 31/Mar/22

Status: Backlog
Project: C# Driver
Component/s: Linq
Affects Version/s: 2.4.2
Fix Version/s: None

Type: Task Priority: Minor - P4
Reporter: Ben Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

I'm attempting to use the new 2.4.2 LINQ features in the aggregation pipeline, but my data structures use a lot of dictionaries that need to be unwound. It looks like the driver does not support $unwind generation for dictionaries. Aggregation stage {$unwind : "$TestDictionary" } words wonders in the shell, but calling the query.ToString() function below throws a System.NotSupportedException:

The collection selector's serializer must implement IBsonArraySerializer: aggregate([]).OfType().Where(to => (to.Name == "TestName")).SelectMany(to => to.TestDictionary)

So I need to pull the whole dictionary locally and then parse it from there. It's not ideal, but there's a workaround. Am I missing something in the attributes or data structure that would allow this to work? If truly not supported, is there a plan to support $unwind via LINQ for Dictionary objects?

Data storage object:

public class TestObject
{
    public string Name {get;set;};
    [BsonDictionaryOptions( DictionaryRepresentation.ArrayOfArrays )]
    public Dictionary<string,string> TestDictionary { get; set; } = new Dictionary<string, string>();
}

Aggregation query:

var query = MongoCollection.AsQueryable()
                .OfType<TestObject>()
                .Where( to => to.Name == "TestName" )
                .SelectMany( to => to.TestDictionary );
 
string queryText = query.ToString();


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