我使用 mongodb [BsonExtraElements] 功能来扩展我的类的一些动态数据,但不幸的是我无法通过 mongodb C# 驱动程序创建查询。
这是我的模型类:
public class MongoProductEntity
{
public MongoProductEntity()
{
AdditionalColumns = new BsonDocument { AllowDuplicateNames = false };
}
[BsonExtraElements]
public BsonDocument AdditionalColumns { get; set; }
public string BrandName { get; set; }
}
这是查询部分:
var productEntity = new MongoProductEntity ()
{
BrandName = "Brand"
};
productEntity.AdditionalColumns.Add("testProperty", 6);
productEntity.AdditionalColumns.Add("testProperty2", "almafa");
await productEntityRepo.InsertAsync(productEntity);
var qq = productEntityRepo.Where(x => x.AdditionalColumns["testProperty"] == 6).ToList();
此查询不会从数据库返回任何元素,但是如果我尝试查询 BrandName 属性,一切正常!
有没有人遇到过类似的情况或知道为什么该查询不起作用?
提前谢谢!
这里只需简单说明一下:productEntityRepo 的类型是 MongoDb MongoProductEntity 集合的包装器,此包装器将集合作为 Queryable 返回,仅此而已。我正在使用 MongoDb 3.2.9 和最新的 C# 驱动程序 2.2.4。
Since C# 驱动程序的 .Inject() 方法可以在FilterDefinition<T>
:
var filter = Builders<BsonDocument>.Filter.Eq("testProperty2", "almafa");
productEntityRepo.Where((dbModel) => dbModel.BrandName == "Brand" && filter.Inject());
这应该允许您表达难以或不可能通过 LINQ 描述的过滤器。不过,您需要从 2.2.4 更新到较新的版本。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)