I have a document type with some attributes, one of which is a Dictionary< string, string >.
Serialization and De-serealization seem to work fine (I can search, perform CRUD operations, etc.). The problem is that I'm trying to write a method to find all the objects of that type where the dictionary contains a specific value (id) among its Keys.
Attempted Query:
var objectCollection = db.GetCollection<MyClass>("MyClass");
var query = Query<MyClass>.Where(m => m.MyDictionary.Any(k => k.Key == id));
The Class:
public class MyClass
{
public ObjectId Id { get; set; }
// ...
[BsonElement("Dictionary")]
public Dictionary<string, string> MyDictionary { get; set; }
}
...but I get this Exception while building the query:
Any requires that the serializer specified for Dictionary support items by implementing
MongoDB.Bson.Serialization.IBsonArraySerializer and returning a non-null result.
MongoDB.Bson.Serialization.Serializers.DictionarySerializer`2[System.String,System.String]
is the current serializer.
I suspect the problem is related to the fact that the c# driver's default serializer doesn't support this. Is there a workaround?
I've also tried to perform a "Contains" for the id
string on the Dictionary.Keys
collection, but that gives a NotSupportedException: Unable to determine the serialization information for the expression: m.Dictionary.Keys.