Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB C#2.x驱动程序与字典的ElemMatch_C#_Mongodb_Mongodb Csharp 2.0 - Fatal编程技术网

MongoDB C#2.x驱动程序与字典的ElemMatch

MongoDB C#2.x驱动程序与字典的ElemMatch,c#,mongodb,mongodb-csharp-2.0,C#,Mongodb,Mongodb Csharp 2.0,我尝试使用ElemMatch在MongoDB中使用2.2驱动程序查找文档,但没有成功。我收到一个例外情况,例如: System.InvalidOperationException:字段的序列化程序 “EnabledForProduct”必须实现IBMSerializer并提供 项目序列化信息 我的班级是这样的: public class Document { public string Id {get; set;} public Dictionary<Product, bool&g

我尝试使用ElemMatch在MongoDB中使用2.2驱动程序查找文档,但没有成功。我收到一个例外情况,例如:

System.InvalidOperationException:字段的序列化程序 “EnabledForProduct”必须实现IBMSerializer并提供 项目序列化信息

我的班级是这样的:

public class Document
{
  public string Id {get; set;}
  public Dictionary<Product, bool> EnabledForProduct { get; set; }
}
public enum Product {Product1,Product2};
公共类文档
{
公共字符串Id{get;set;}
公共字典启用的orproduct{get;set;}
}
公共枚举产品{Product1,Product2};
我的类地图如下所示:

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>()));
});
BsonClassMap.RegisterClassMap(cm=>
{
cm.AutoMap();
cm.MapMember(c=>c.EnabledForProduct)
.SetSerializer(新字典接口实现程序)Serializer(字典表示.ArrayOfDocuments,
BsonSerializer.LookupSerializer(),
LookupSerializer());
});
尝试使用以下筛选器时发生异常:

Builders<Document>.Filter.ElemMatch(f => f.EnabledForProduct,
    x => x.Key == Product1 && x.Value))
Builders.Filter.ElemMatch(f=>f.EnabledForProduct,
x=>x.Key==Product1&&x.Value))
这曾经在1.x驱动程序中完美地工作


有人知道我做错了什么吗?

好吧,在一些尝试和错误实现之后,我找到了一种方法来做我需要的事情。我没有直接使用模型类,而是为ElemMatch过滤器使用了BsonDocument集合,如下所示:

var bsonCollection = database.GetCollection<BsonDocument>("testcollection");
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)));
var bsonCollection=database.GetCollection(“testcollection”);
过滤器的创建方式如下:

var bsonCollection = database.GetCollection<BsonDocument>("testcollection");
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)));
var filter=Builders.filter.ElemMatch(“EnabledForProduct”,Builders.filter.And(Builders.filter.Eq(“k”,int)Product.Product1),Builders.filter.Eq(“v”,true));
并且可以使用BsonSerializer将泛型BsonDocument反序列化回我的模型类:

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