3

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?

1 Answers1

3

Well, after some trial and error implementations, I figured out a way to do what I needed. Instead of directly using my model class, I ended up using a BsonDocument collection just for my ElemMatch filter like this:

var bsonCollection = database.GetCollection<BsonDocument>("testcollection");

The filter gets created like this:

var filter = Builders<BsonDocument>.Filter.ElemMatch("EnabledForProduct", Builders<BsonDocument>.Filter.And(Builders<BsonDocument>.Filter.Eq("k",(int)Product.Product1),Builders<BsonDocument>.Filter.Eq("v",true)));

And the generic BsonDocument can be deserialized back to my model class using BsonSerializer:

var foundDoc = BsonSerializer.Deserialize<Document>(bsonCollection.Find(filter).Limit(1).FirstOrDefault());