I’m having trouble filtering on a property of my class which is a Dictionary. When I try to query it with something like the following:
Builders<MyClass>.Filter.Eq(x => x.Metadata["somekey"], "some value");
Then I get an error like the following Unable to determine the serialization information for x => x.Metadata.get_Item("somekey")
I know that the ability to query an array of documents dictionary was introduced around 2.3 (I’m on 2.5), but I can’t seem to get it to work.
I found these related JIRA tickets:
https://jira.mongodb.org/browse/CSHARP-917
https://jira.mongodb.org/browse/CSHARP-1521
Here is a full representation of my class for context.
public class MyClass
{
[BsonDictionaryOptions(DictionaryRepresentation.ArrayOfDocuments)]
public Dictionary<string, object> Metadata { get; set; }
}
Okay, so digging a little deeper, I see that the querying of ArrayOfDocuments solved in CSHARP-1521 allowed querying them using ElemMatch (at least that’s what the test suggests). I’m wondering is there logical restrictions here that the C# driver couldn’t translate my above query for me to:
{ Metadata: { $elemMatch: { k: "somekey", v: "some value" } } }
I understand that the first property should point to a property on the object, however in this case, its more like a partial evaluation of query, e.g. where we usually expect to build an expression tree like:
x => x.Metadata.somekey
we’re building something like:
x => x.Metadata.Any(m => m.Key =="somekey" && [INJECT OTHER HALF OF FILTER HERE])
Does the C# driver team have any comments on this, or is it something that we can work towards a solution so that Dictionaries have a consistent query approach despite their underlying structure?