I am trying to use ElemMatch to find a document in MongoDB using the 2.2 driver with no success. I am receiving an exception such as:
System.InvalidOperationException : The serializer for field 'EnabledForProduct' must implement IBsonArraySerializer and provide item serialization info.
Here's how my class looks like:
public class Document
{
public string Id {get; set;}
public Dictionary<Product, bool> EnabledForProduct { get; set; }
}
public enum Product {Product1,Product2};
My ClassMap looks lke this:
BsonClassMap.RegisterClassMap<Document>(cm =>
{
cm.AutoMap();
cm.MapMember(c => c.EnabledForProduct)
.SetSerializer(new DictionaryInterfaceImplementerSerializer<Dictionary<Product, bool>>(DictionaryRepresentation.ArrayOfDocuments,
BsonSerializer.LookupSerializer<int>(),
BsonSerializer.LookupSerializer<bool>()));
});
The exception occurs when trying to use a Filter such as:
Builders<Document>.Filter.ElemMatch(f => f.EnabledForProduct,
x => x.Key == Product1 && x.Value))
This used to work flawlessly in the 1.x Driver.
Does anyone know what I am doing wrong?