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();