使用 C# mongo 驱动程序构建查询
Build query with C# mongo driver
我想知道如何使用 mongo 驱动程序构建复杂的查询。这是我的查询:
var builder = Builders<MongoNavFilter>.Filter;
var query = builder.Where(x => x.Link == link && x.SubLink == subLink);
if (some statement)
{
var finalExpression = ...
query = query & Builders<MongoNavFilter>.Filter.Where(finalExpression);
}
if (onsale)
query = query & Builders<MongoNavFilter>.Filter.Where(x => !(x.Promo == null && x.Promo == string.Empty));
var filters = _db.GetCollection<MongoNavFilter>("NavFilters").
Find(query).ToList();
使用 EF
IQueryable
这段代码工作正常,但我无法使用 mongo driver
构建正确的查询。
谁能告诉我我做错了什么?
了解 finalExpression
是什么会有所帮助,以防出现问题。在我看来,mongodb 驱动程序对 !(x.Promo == null && x.Promo == string.Empty)
的翻译可能存在问题
你能试试下面的代码而不是你的代码,看看它是否会有所作为
var builder = Builders<MongoNavFilter>.Filter;
var query = builder.Eq(x => x.Link, link) & builder.Eq(x=> x.SubLink, subLink);
if (some statement)
{
var finalExpression = ... // write this using the same syntax
query = query & filter;
}
if (onsale)
query = query & builder.Ne(x=>x.Promo, null) & builder.Ne(x=> x. Promo, string.Empty);
var filters = _db.GetCollection<MongoNavFilter>("NavFilters").
Find(query).ToList();
我想知道如何使用 mongo 驱动程序构建复杂的查询。这是我的查询:
var builder = Builders<MongoNavFilter>.Filter;
var query = builder.Where(x => x.Link == link && x.SubLink == subLink);
if (some statement)
{
var finalExpression = ...
query = query & Builders<MongoNavFilter>.Filter.Where(finalExpression);
}
if (onsale)
query = query & Builders<MongoNavFilter>.Filter.Where(x => !(x.Promo == null && x.Promo == string.Empty));
var filters = _db.GetCollection<MongoNavFilter>("NavFilters").
Find(query).ToList();
使用 EF
IQueryable
这段代码工作正常,但我无法使用 mongo driver
构建正确的查询。
谁能告诉我我做错了什么?
了解 finalExpression
是什么会有所帮助,以防出现问题。在我看来,mongodb 驱动程序对 !(x.Promo == null && x.Promo == string.Empty)
你能试试下面的代码而不是你的代码,看看它是否会有所作为
var builder = Builders<MongoNavFilter>.Filter;
var query = builder.Eq(x => x.Link, link) & builder.Eq(x=> x.SubLink, subLink);
if (some statement)
{
var finalExpression = ... // write this using the same syntax
query = query & filter;
}
if (onsale)
query = query & builder.Ne(x=>x.Promo, null) & builder.Ne(x=> x. Promo, string.Empty);
var filters = _db.GetCollection<MongoNavFilter>("NavFilters").
Find(query).ToList();