MongoDB-组合多个数值范围查询(C#驱动程序)
*这里是Mongo新手 我有一个包含数百个数字字段的文档,需要组合查询MongoDB-组合多个数值范围查询(C#驱动程序),mongodb,mongodb-.net-driver,Mongodb,Mongodb .net Driver,*这里是Mongo新手 我有一个包含数百个数字字段的文档,需要组合查询 var collection = _myDB.GetCollection<MyDocument>("collection"); IMongoQuery mongoQuery; // = Query.GT("field", value1).LT(value2); foreach (MyObject queryObj in Queries) { // I have several hundred fields
var collection = _myDB.GetCollection<MyDocument>("collection");
IMongoQuery mongoQuery; // = Query.GT("field", value1).LT(value2);
foreach (MyObject queryObj in Queries)
{
// I have several hundred fields such as Height, that are in queryObj
// how do I build a "boolean" query in C#
mongoQuery = Query.GTE("Height", Convert.ToInt16(queryObj.Height * lowerbound));
}
var collection=\u myDB.GetCollection(“collection”);
IMongoQuery mongoQuery;//=Query.GT(“字段”,value1).LT(value2);
foreach(查询中的MyObject查询对象)
{
//我有几百个字段,比如高度,都在queryObj中
//如何在C#中构建“布尔”查询
mongoQuery=Query.GTE(“Height”,Convert.ToInt16(queryObj.Height*lowerbound));
}
我在queryObj中有几百个字段,例如高度(例如宽度、面积、周长等),如何在C#中构建一个“布尔”查询,将每个字段的范围查询结合起来
我尝试使用示例Query.GT(“field”,value1).LT(value2);,但是,编译器不接受LT(Value)构造。在任何情况下,我都需要能够通过循环遍历每个数值字段值来构建复杂的布尔查询
感谢您帮助一位新手。编辑3:
var props = ["Height", "Weight", "Age"];
var query = _myDb.GetCollection<MyDoc>("CName").AsQueryable<MyDoc>();
foreach (var prop in props)
{
query = query.Where(prop, GetLowerLimit(queryObj, prop), WhereOperation.Between, GetUpperLimit(queryObj, prop));
}
// the above query when iterated over, will result in a where clause that joins each individual `prop\condition` with an `AND`.
// The code above will not compile. The `Where` function I wrote doesnt accept 4 parameters. You will need to implement the logic for that yourself. Though it ought to be straight forward I think...
好的,看起来您已经准备好了构建复杂查询的代码。在这种情况下,您只需要修复编译器问题。我假设你想做以下(x>20&&x<40)&&(y>30&&y<50)
var collection = _myDB.GetCollection<MyDocument>("collection");
var queries = new List<IMongoQuery>();
foreach (MyObject queryObj in Queries)
{
//I have several hundred fields such as Height, that are in queryObj
//how do I build a "boolean" query in C#
var lowerBoundQuery = Query.GTE("Height", Convert.ToInt16(queryObj.Height * lowerbound));
var upperBoundQuery = Query.LTE("Height", Convert.ToInt16(queryObj.Height * upperbound));
var query = Query.And(lowerBoundQuery, upperBoundQuery);
queries.Add(query);
}
var finalQuery = Query.And(queries);
/*
if you want to instead do an OR,
var finalQuery = Query.Or(queries);
*/
目前它只支持
==
&&=代码>,但实现=
或编辑3:
var props = ["Height", "Weight", "Age"];
var query = _myDb.GetCollection<MyDoc>("CName").AsQueryable<MyDoc>();
foreach (var prop in props)
{
query = query.Where(prop, GetLowerLimit(queryObj, prop), WhereOperation.Between, GetUpperLimit(queryObj, prop));
}
// the above query when iterated over, will result in a where clause that joins each individual `prop\condition` with an `AND`.
// The code above will not compile. The `Where` function I wrote doesnt accept 4 parameters. You will need to implement the logic for that yourself. Though it ought to be straight forward I think...
好的,看起来您已经准备好了构建复杂查询的代码。在这种情况下,您只需要修复编译器问题。我假设你想做以下(x>20&&x<40)&&(y>30&&y<50)
var collection = _myDB.GetCollection<MyDocument>("collection");
var queries = new List<IMongoQuery>();
foreach (MyObject queryObj in Queries)
{
//I have several hundred fields such as Height, that are in queryObj
//how do I build a "boolean" query in C#
var lowerBoundQuery = Query.GTE("Height", Convert.ToInt16(queryObj.Height * lowerbound));
var upperBoundQuery = Query.LTE("Height", Convert.ToInt16(queryObj.Height * upperbound));
var query = Query.And(lowerBoundQuery, upperBoundQuery);
queries.Add(query);
}
var finalQuery = Query.And(queries);
/*
if you want to instead do an OR,
var finalQuery = Query.Or(queries);
*/
目前它只支持==
&&=代码>,但是实现=
或嗨应该不会那么困难,谢谢您的响应,但是我如何才能建立一个范围列表,例如Where(x=>x.Height>20&&x.Height<40)、y=>y.Height>30&&y.Height<60)等等……最好有一个非LINQ方法吗?或者LINQ是我唯一的选择?编辑是否有帮助。。因为,老实说,我不明白你在评论中写的例子在哪里。它不会编译其中
不接受两个参数x
和y
…添加了代码以使用IMongoQuery
。请参阅edit2
edit3
应该可以解决您遇到的编译器问题。您好,感谢您的回复,但是我如何建立一个范围列表,例如Where(x=>x.Height>20&&x.Height<40)、y=>y.Height>30&&y.Height<60)等等……最好有非LINQ方法吗?或者LINQ是我唯一的选择?编辑是否有帮助。。因为,老实说,我不明白你在评论中写的例子在哪里。它不会编译其中
不接受两个参数x
和y
…添加了代码以使用IMongoQuery
。请参阅edit2
edit3
应修复您遇到的编译器问题。
// I think this might be deprecated. Please refer the release notes for the C# driver version 1.5.0
Query.And(Query.GTE("Salary", new BsonDouble(20)), Query.LTE("Salary", new BsonDouble(40)), Query.GTE("Height", new BsonDouble(20)), Query.LTE("Height", new BsonDouble(40)))
// strongly typed version
new QueryBuilder<Employee>().And(Query<Employee>.GTE(x => x.Salary, 40), Query<Employee>.LTE(x => x.Salary, 60), Query<Employee>.GTE(x => x.HourlyRateToClients, 40), Query<Employee>.LTE(x => x.HourlyRateToClients, 60))