zoukankan      html  css  js  c++  java
  • 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();

  • 相关阅读:
    mysql分表技术
    TP5.0 excel 导入导出
    整理:手机端弹出提示框,使用的bootstrap中的模态框(modal,弹出层),比kendo弹出效果好
    Bootstrap表单验证插件bootstrapValidator使用方法整理
    input属性为number时,如何去掉+、-号?
    input 属性为 number,maxlength不起作用如何解决?
    mysql给root开启远程访问权限
    thinkphp——通过在线编辑器添加的内容在模板里正确显示(只显示内容,而不是html代码)
    解决网站请求速度慢的一些方法
    JS封闭函数、闭包、内置对象
  • 原文地址:https://www.cnblogs.com/xadmin/p/6236707.html
Copyright © 2011-2022 走看看