[CSHARP-2509] Unsupported filter when attempting a ContainsValue on a Dictionary Created: 08/Feb/19  Updated: 31/Mar/22

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

Type: New Feature Priority: Major - P3
Reporter: Alex Bevilacqua Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: rp-track
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Case:

 Description   

Tested using version 2.7.3 of the C# driver.

Issue appears to be in MongoDB.Driver.Linq.Translators.PredicateTranslator.Translate. 

Given the following document:

{
    "_id" : ObjectId("5c5c8d915018e66f49cf8b59"),
    "UserAttributes" : {
        "CustomerClass" : "Schools",
        "CustomerSubClass" : "Law Schools"
    },
    "UserId" : "urn:idprofile:PA10000"
} 

 

this code will fail:

public class User
{
 public ObjectId _id;
 public string UserId { get; set; }
public Dictionary<string, string> UserAttributes = new Dictionary<string, string>();
}
var uri = "xxx";
var mongoURL = new MongoUrl(uri);
var client = new MongoClient(mongoURL);
var database = client.GetDatabase("csharp");
var Users = database.GetCollection<User>("Users");
int count = Users.AsQueryable().Where(x => x.UserAttributes.ContainsValue("Law Schools")).Count(); 

 

The error that is produced:

System.ArgumentException HResult=0x80070057 Message=Unsupported filter: \{document}{UserAttributes}.ContainsValue("Law Schools"). Source=MongoDB.Driver StackTrace: at MongoDB.Driver.Linq.Translators.PredicateTranslator.Translate(Expression node) at MongoDB.Driver.Linq.Translators.PredicateTranslator.Translate(Expression node, IBsonSerializerRegistry serializerRegistry) at MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslateWhere(WhereExpression node) at MongoDB.Driver.Linq.Translators.QueryableTranslator.Translate(Expression node) at MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslateGroupBy(GroupByExpression node) at MongoDB.Driver.Linq.Translators.QueryableTranslator.Translate(Expression node) at MongoDB.Driver.Linq.Translators.QueryableTranslator.TranslatePipeline(PipelineExpression node) at MongoDB.Driver.Linq.Translators.QueryableTranslator.Translate(Expression node) at MongoDB.Driver.Linq.Translators.QueryableTranslator.Translate(Expression node, IBsonSerializerRegistry serializerRegistry, ExpressionTranslationOptions translationOptions) at MongoDB.Driver.Linq.MongoQueryProviderImpl`1.Translate(Expression expression) at MongoDB.Driver.Linq.MongoQueryProviderImpl`1.Execute(Expression expression) at MongoDB.Driver.Linq.MongoQueryProviderImpl`1.Execute[TResult](Expression expression) at System.Linq.Queryable.Count[TSource](IQueryable`1 source) at ConsoleApp1.Program.Test2() in C:\Workspace\ConsoleApp1\ConsoleApp1\Program.cs:line 77 at ConsoleApp1.Program.Main(String[] args) in C:\Workspace\ConsoleApp1\ConsoleApp1\Program.cs:line 31



 Comments   
Comment by Wan Bachtiar [ 06/Mar/19 ]

Based on the code, currently only Contains and ContainsKey are supported. We need to add support for ContainsValue with it's associated TranslateContainsValue() method in Linq/Translators/PredicateTranslator.cs

Regards,
Wan.

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