我希望有可能在运行时添加查询。我的解决方案与#34; old"司机是这样的。
// A List does hold the queries
List<IMongoQuery> QueryConditionList = new List<IMongoQuery>();
void AddQuery(string sType, string sField, string sValue)
{
// I can add of course several Queries
if (sType == "EQ")
QueryConditionList.Add(Query.EQ(sField, sValue));
else if (sType == "GT")
QueryConditionList.Add(Query.GT(sField, sValue));
else if (sType == "LT")
QueryConditionList.Add(Query.LT(sField, sValue));
}
// At some point you can execute the queries
void ExecuteQuery()
{
// Combine all to on "And" Query ("Or" would be possible as well)
IMongoQuery query = Query.And(QueryConditionList);
// Then I can get my cursor with a Find
MongoCursor<BsonDocument> mc = MoCollection.Find(query);
// Do anything with the cursor...
}
这确实很有效。但我不知道如何使用新语法来做到这一点。 我的所有方法都不是真正的动态。像:
var builder = Builders<BsonDocument>.Filter;
var filter = builder.Eq(Field1, Value1) & builder.Eq(Field2, Value2);
我想我可以添加一些像
这样的过滤器filter.add(builder.Eq(Field3, Value3)); // But of course I can't