MongoDB C#驱动中查询两个值的区间怎么写

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MongoDB C#驱动中查询两个值的区间怎么写相关的知识,希望对你有一定的参考价值。

this.collectionAlarm.Find(Query.And( Query.GTE("Time", BsonDateTime.Create(start)), Query.LTE("Time", BsonDateTime.Create(end)), Query.GTE("Latitude", BsonString.Create(l_corr[1])), Query.LTE("Latitude", BsonString.Create(r_corr[1])), Query.GTE("Longitude", BsonString.Create(l_corr[0])), 只用时间查询可以出来数据,但是加上对另外两个参数就没数据了。这俩参数都是对区间判断的。

参考技术A success本回答被提问者采纳

mongodb C# 驱动查询

 

INoSqlProvider provider = NoSqlManager.Create("CloudTable");
IMongoCollection<FormMongoDBModel> collection = provider.GetCollection<FormMongoDBModel, IMongoCollection<FormMongoDBModel>>(ctid);

#region 查询条件拼接
switch (listquerymodel.querytype)
{
case "equal": //等于
if (listquerymodel.fieldtype == "Datetime")
{
list.Add(Builders<FormMongoDBModel>.Filter.Eq("field." + listquerymodel.field + "."+ queryconditiontype, Convert.ToDateTime(listquerymodel.value)));
}
else if (listquerymodel.fieldtype == "Number" || listquerymodel.fieldtype == "Money" || listquerymodel.fieldtype == "Formula")
{
list.Add(Builders<FormMongoDBModel>.Filter.Eq("field." + listquerymodel.field + "."+ queryconditiontype, Convert.ToDouble((listquerymodel.value))));
}
else
{
list.Add(Builders<FormMongoDBModel>.Filter.Eq("field." + listquerymodel.field + "."+ queryconditiontype, listquerymodel.value));
}
break;
case "notequal": //不等于
list.Add(Builders<FormMongoDBModel>.Filter.Ne("field." + listquerymodel.field + "."+ queryconditiontype, listquerymodel.value));
break;
case "like": //包含
list.Add(Builders<FormMongoDBModel>.Filter.Regex("field." + listquerymodel.field + "."+ queryconditiontype, listquerymodel.value));
break;
case "orlike":
string[] orlike_values = listquerymodel.value.Split(‘|‘);
List<FilterDefinition<FormMongoDBModel>> orlikeList = new List<FilterDefinition<FormMongoDBModel>>();
for (int i = 0; i < orlike_values.Length; i++)
{
orlikeList.Add(Builders<FormMongoDBModel>.Filter.Regex("field." + listquerymodel.field + "."+ queryconditiontype, orlike_values[i]));
}
list.Add(Builders<FormMongoDBModel>.Filter.Or(orlikeList));
break;
case "andlike":
string[] andlike_values = listquerymodel.value.Split(‘|‘);
List<FilterDefinition<FormMongoDBModel>> andlike_value = new List<FilterDefinition<FormMongoDBModel>>();
for (int i = 0; i < andlike_values.Length; i++)
{
list.Add(Builders<FormMongoDBModel>.Filter.Regex("field." + listquerymodel.field + "."+ queryconditiontype, andlike_values[i]));
}
break;
case "notlike":
string[] notlike_values = listquerymodel.value.Split(‘|‘);
List<FilterDefinition<FormMongoDBModel>> notlikeList = new List<FilterDefinition<FormMongoDBModel>>();
for (int i = 0; i < notlike_values.Length; i++)
{
list.Add(Builders<FormMongoDBModel>.Filter.Not(Builders<FormMongoDBModel>.Filter.Regex("field." + listquerymodel.field + "."+ queryconditiontype, notlike_values[i])));
}
break;
case "null":
FilterDefinition<FormMongoDBModel> filter = new BsonDocument("field." + listquerymodel.field + "."+ queryconditiontype, BsonNull.Value);
list.Add(Builders<FormMongoDBModel>.Filter.And(filter));
break;
case "notnull":
list.Add(Builders<FormMongoDBModel>.Filter.Ne("field." + listquerymodel.field + "."+ queryconditiontype, BsonNull.Value));
break;
case "or":
string[] or_values = listquerymodel.value.Split(‘|‘);
List<FilterDefinition<FormMongoDBModel>> orList = new List<FilterDefinition<FormMongoDBModel>>();
for (int i = 0; i < or_values.Length; i++)
{
orList.Add(new BsonDocument("field." + listquerymodel.field + "."+ queryconditiontype, or_values[i]));
}
list.Add(Builders<FormMongoDBModel>.Filter.Or(orList));
break;
case "greater":
if (listquerymodel.fieldtype == "Datetime")
{
list.Add(Builders<FormMongoDBModel>.Filter.Gt("field." + listquerymodel.field + "."+ queryconditiontype, Convert.ToDateTime(listquerymodel.value)));
}
else
{
list.Add(Builders<FormMongoDBModel>.Filter.Gt("field." + listquerymodel.field + "."+ queryconditiontype, listquerymodel.value));
}
break;
case "greaterequal":
if (listquerymodel.fieldtype == "Datetime")
{
list.Add(Builders<FormMongoDBModel>.Filter.Gte("field." + listquerymodel.field + "."+ queryconditiontype, Convert.ToDateTime(listquerymodel.value)));
}
else
{
list.Add(Builders<FormMongoDBModel>.Filter.Gte("field." + listquerymodel.field + "."+ queryconditiontype, listquerymodel.value));
}
break;
case "less":
if (listquerymodel.fieldtype == "Datetime")
{
list.Add(Builders<FormMongoDBModel>.Filter.Lt("field." + listquerymodel.field + "."+ queryconditiontype, Convert.ToDateTime(listquerymodel.value)));
}
else
{
list.Add(Builders<FormMongoDBModel>.Filter.Lt("field." + listquerymodel.field + "."+ queryconditiontype, listquerymodel.value));
}
break;
case "lessequal":
if (listquerymodel.fieldtype == "Datetime")
{
list.Add(Builders<FormMongoDBModel>.Filter.Lte("field." + listquerymodel.field + "."+ queryconditiontype, Convert.ToDateTime(listquerymodel.value)));
}
else
{
list.Add(Builders<FormMongoDBModel>.Filter.Lte("field." + listquerymodel.field + "."+ queryconditiontype, listquerymodel.value));
}
break;
case "between":
string[] between_values = listquerymodel.value.Split(‘|‘);
string between_value1 = between_values[0];
string between_value2 = between_values[1];
if (listquerymodel.fieldtype == "Datetime")
{
list.Add(Builders<FormMongoDBModel>.Filter.Gt("field." + listquerymodel.field + "."+ queryconditiontype, Convert.ToDateTime(between_value1)));
list.Add(Builders<FormMongoDBModel>.Filter.Lt("field." + listquerymodel.field + "."+ queryconditiontype, Convert.ToDateTime(between_value2)));
}
else
{
list.Add(Builders<FormMongoDBModel>.Filter.Gt("field." + listquerymodel.field + "."+ queryconditiontype, Convert.ToInt32(between_value1)));
list.Add(Builders<FormMongoDBModel>.Filter.Lt("field." + listquerymodel.field + "."+ queryconditiontype, Convert.ToInt32(between_value2)));
}
break;

default:
break;
}

#endregion

 

result = collection.Find(Builders<FormMongoDBModel>.Filter.And(list)).Skip(pageskip).Limit(pagesize).ToList();


























































































































以上是关于MongoDB C#驱动中查询两个值的区间怎么写的主要内容,如果未能解决你的问题,请参考以下文章

如何在 2.1 MongoDB C# 驱动程序中使用地理空间查询?

mongodb C# 驱动查询

kettle中mongodb查询语句怎么写

C# MongoDB 驱动程序 2.0 - 从近处查询返回距离

为啥 mongodb 查询在数组中查找具有两个值的文档不起作用?

使用 MongoDB C# 驱动程序 2.2 对嵌套字段进行强类型查询