private FilterDefinition<FacilityEntity> GetFilterDefinition(SearchFacilityRequest request) { var filterBuilder = Builders<FacilityEntity>.Filter;//条件过滤构建器 var filters = GetFilters(request, filterBuilder); return filterBuilder.And(filters); } private IEnumerable<FilterDefinition<FacilityEntity>> GetFilters(SearchFacilityRequest request, FilterDefinitionBuilder<FacilityEntity> filterBuilder) { var filters = new List<FilterDefinition<FacilityEntity>> { filterBuilder.Exists("Id") }; if (request.LayerIds.Any()) { filters.Add(filterBuilder.In("LayerId", request.LayerIds)); } if (request.AreaCodes.Any()) { filters.Add(filterBuilder.In("AreaCode", request.AreaCodes)); } if (!string.IsNullOrEmpty(request.KeyWord)) { filters.Add(filterBuilder.Where(x => x.Name.Contains(request.KeyWord)));//模糊匹配 } return filters; }
Query.And(Query.EQ("name", "a"), Query.EQ("title", "t"));//同时满足多个条件
Query.EQ("name", "a");//等于
Query.Exists("type", true);//判断键值是否存在
Query.GT("value", 2);//大于>
Query.GTE("value", 3);//大于等于>=
Query.In("name", "a", "b");//包括指定的所有值,可以指定不同类型的条件和值
Query.LT("value", 9);//小于<
Query.LTE("value", 8);//小于等于<=
Query.Mod("value", 3, 1);//将查询值除以第一个给定值,若余数等于第二个给定值则返回该结果
Query.NE("name", "c");//不等于
Query.Nor(Array);//不包括数组中的值
Query.Not("name");//元素条件语句
Query.NotIn("name", "a", 2);//返回与数组中所有条件都不匹配的文档
Query.Or(Query.EQ("name", "a"), Query.EQ("title", "t"));//满足其中一个条件
Query.Size("name", 2);//给定键的长度
Query.Type("_id", BsonType.ObjectId );//给定键的类型
Query.Where(BsonJavaScript);//执行JavaScript