Ejemplo n.º 1
0
        /// <summary>
        /// 生成查询过滤器
        /// </summary>
        /// <param name="jsonString"></param>
        /// <returns></returns>
        protected FilterDefinition <BsonDocument> FilterConvertor(string jsonString)
        {
            if (!string.IsNullOrWhiteSpace(jsonString) && jsonString.Contains("{") && jsonString.Contains("}")) ///若数据可以转换
            {
                var filterDict    = Convertor.FromJsonToDict2(jsonString);                                      ///转化成字典
                var filterBuilder = Builders <BsonDocument> .Filter;
                var filter        = filterBuilder.Empty;
                foreach (var item in filterDict)
                {
                    var key   = item.Key;
                    var value = item.Value;
                    if ("_id" == key.ToLower())
                    {
                        value = ObjectId.Parse(value.ToString());
                    }
                    else if (StringChecker.IsGUID(value.ToString()))
                    {
                        value = Guid.Parse(value.ToString());
                    }
                    else if (value is string && value.ToString().Contains("new Date"))
                    {
                        value = Convert.ToDateTime(value.ToString().Replace("new Date", string.Empty).Replace("(", string.Empty).Replace(")", string.Empty).Replace("'", string.Empty));
                    }


                    if (value is Dictionary <string, object> && (value as Dictionary <string, object>).ContainsKey("$gt"))
                    {
                        var temp = (value as Dictionary <string, object>)["$gt"];
                        var val  = new object();
                        if (temp is string && temp.ToString().Contains("new Date"))
                        {
                            val = Convert.ToDateTime(temp.ToString().Replace("new Date", string.Empty).Replace("(", string.Empty).Replace(")", string.Empty).Replace("'", string.Empty));
                        }
                        filter &= filterBuilder.Gt(key, val);
                    }
                    else if (value is Dictionary <string, object> && (value as Dictionary <string, object>).ContainsKey("$lt"))
                    {
                        var temp = (value as Dictionary <string, object>)["$lt"];
                        var val  = new object();
                        if (temp is string && temp.ToString().Contains("new Date"))
                        {
                            val = Convert.ToDateTime(temp.ToString().Replace("new Date", string.Empty).Replace("(", string.Empty).Replace(")", string.Empty).Replace("'", string.Empty));
                        }
                        filter &= filterBuilder.Lt(key, val);
                    }
                    else
                    {
                        filter &= filterBuilder.Eq(key, value);
                    }
                }


                if ("{}" == jsonString)
                {
                    return(Builders <BsonDocument> .Filter.Empty);
                }

                //filter = filterBuilder.Eq("Status", "待执行") & filterBuilder.Gt("StartTime", DateTime.Now);
                return(filter);
            }
            return(null);
        }