[CSHARP-4317] Linq Provider V3 throws error when accessing dictionary value Created: 09/Sep/22  Updated: 14/Sep/22  Resolved: 14/Sep/22

Status: Closed
Project: C# Driver
Component/s: LINQ3
Affects Version/s: 2.17.1
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Markus Wildgruber Assignee: James Kovacs
Resolution: Duplicate Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Attachments: Text File Program.cs     File my_collection.json    
Issue Links:
Duplicate
duplicates CSHARP-4316 LINQ3 incorrect translate for type li... Closed

 Description   

Summary

When storing dictionaries as ArrayOfDocuments ([k: 1, v: "v1"}, \{k: 2, v: "v2"}] and trying to access the values of the dictionary, this works for Linq Provider V2, but version 3 throws an ExpressionNotSupported exception with "Expression not supported: y.Value.". 

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

C# Driver: 2.17.1

MongoDB 5.0.9 Community

How to Reproduce

Please see attached code file for an extensive sample. 

Code:

        var settings = MongoClientSettings.FromConnectionString("mongodb://localhost:27017");
        settings.LinqProvider = prov;
        var client = new MongoClient(settings);
        var db = client.GetDatabase("test");
        var coll = db.GetCollection<MyDocument>("my_collection");
        var result = coll
            .AsQueryable()
            .Select(x => new
            {
                Strings = x.DictData.Select(y => y.Value),
            })
            .ToArray();
        Console.WriteLine($"Works for provider version {prov} (returned items: {result.Length})");
        foreach (var item in result)
            Console.WriteLine(string.Join("; ", item.Strings));

 

If prov == V2, output is as expected: 

Works for provider version V2 (returned items: 2)
Value10; Value11; Value13
Value20; Value21; Value23

If prov == V3, exception is thrown.

 

Additional Background

The real live example is a more complex linq query with GroupBy statements that build aggregates based on the dictionary values etc. Problem can be circumvented for the moment by using V2, but we want to use V3 whenever possible.



 Comments   
Comment by Githook User [ 14/Sep/22 ]

Author:

{'name': 'James Kovacs', 'email': 'jkovacs@post.harvard.edu', 'username': 'JamesKovacs'}

Message: CSHARP-4317: Added integration test demonstrating that CSHARP-4316 fix also resolved dictionary key/value projection issue. (#877)
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/f28c86ab88bddfb0581b218f266c121e4461884e

Comment by James Kovacs [ 14/Sep/22 ]

Resolving CSHARP-4316 also resolved this issue. y.Value was being interpreted as the value accessor for a nullable type causing query translation to fail. Interestingly y.Key works even with the 2.17.1 driver correctly translating to $DictData.k.

Comment by Boris Dogadov [ 09/Sep/22 ]

Thanks m.wildgruber@sevantage.de for reporting this.
This issue might be related to this ticket https://jira.mongodb.org/browse/CSHARP-4316.
We'll be looking into this issue, please follow this ticket for further updates.

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