Overview
在本指南中,您可以学习;了解如何使用构建者为Java Reactive Streams驾驶员中的查询指定筛选器。
构建器是Java Reactive Streams驾驶员提供的类,可帮助您构建BSON对象。要学习;了解更多信息,请参阅构建者指南。
筛选器是用于根据指定条件限制查询结果的操作。 筛选器是在collection中查找与搜索条件匹配的信息的有用工具。
您可以在以下位置使用筛选器:
作为
find()方法的参数在聚合管道的匹配阶段
作为
deleteOne()或deleteMany()方法的参数作为
updateOne()或updateMany()方法的参数
使用筛选器的查询结果的部分示例如下:
费用超过 0 美元但低于 25 美元的商品
不含麸质且热量低于 500 卡路里的食物
提及“辛辣”的美食评论家评论
本指南通过以下类型的操作符示例向您展示如何使用构建者:
Filters类为所有MongoDB查询操作符提供静态工厂方法。每个方法都返回一个BSON类型的实例,您可以将其传递给任何需要查询过滤的方法。
提示
样本数据
本指南中的示例使用以下示例集合:
集合: paint_purchases
{ "_id": 1, "color": "red", "qty": 5, "vendor": ["A"] } { "_id": 2, "color": "purple", "qty": 10, "vendor": ["C", "D"] } { "_id": 3, "color": "blue", "qty": 8, "vendor": ["B", "A"] } { "_id": 4, "color": "white", "qty": 6, "vendor": ["D"] } { "_id": 5, "color": "yellow", "qty": 11, "vendor": ["A", "B"] } { "_id": 6, "color": "pink", "qty": 5, "vendor": ["C"] } { "_id": 7, "color": "green", "qty": 8,"vendor": ["B", "C"] } { "_id": 8, "color": "orange", "qty": 7, "vendor": ["A", "D"] }
集合: binary_numbers
{ "_id": 9, "a": 54, "binaryValue": "00110110" } { "_id": 10, "a": 20, "binaryValue": "00010100" } { "_id": 11, "a": 68, "binaryValue": "1000100" } { "_id": 12, "a": 102, "binaryValue": "01100110" }
集合: geo_points
{ "_id": 13, "coordinates": { "type": "Point", "coordinates": [2.0, 2.0] } } { "_id": 14, "coordinates": { "type": "Point", "coordinates": [5.0, 6.0] } } { "_id": 15, "coordinates": { "type": "Point", "coordinates": [1.0, 3.0] } } { "_id": 16, "coordinates": { "type": "Point", "coordinates": [4.0, 7.0] } }
对比
比较过滤器包括将文档中的值与指定值进行比较的所有操作符。
比较操作符方法包括:
比较方法 | matches |
|---|---|
等于指定值的值。 | |
值大于指定值。 | |
值小于或等于指定值。 | |
值小于指定的值。 | |
值小于或等于指定的值。 | |
值不等于指定的值。 | |
数组中指定的任何值。 | |
没有数组中指定的值。 | |
所有文档。 |
以下示例创建一个筛选器,用于匹配 paint_purchases 集合中 qty 字段的值等于“5 ”的所有文档:
Bson equalComparison = eq("qty", 5); FindPublisher<Document> findPublisher = collection.find(equalComparison); Flux.from(findPublisher) .doOnNext(doc -> System.out.println(doc.toJson())) .blockLast();
{ "_id": 1, "color": "red", "qty": 5, "vendor": ["A"] } { "_id": 6, "color": "pink", "qty": 5, "vendor": ["C"] }
以下示例创建一个过滤器,该过滤器匹配 paint_purchases 集合中 qty 字段的值大于或等于“10”的所有文档:
Bson gteComparison = gte("qty", 10); FindPublisher<Document> findPublisher = collection.find(gteComparison); Flux.from(findPublisher) .doOnNext(doc -> System.out.println(doc.toJson())) .blockLast();
{ "_id": 2, "color": "purple", "qty": 10, "vendor": ["C", "D"] } { "_id": 5, "color": "yellow", "qty": 11, "vendor": ["A", "B"] }
以下示例创建一个筛选器,用于匹配 paint_purchases 集合中所有文档,因为谓词为空:
Bson emptyComparison = empty(); FindPublisher<Document> findPublisher = collection.find(emptyComparison); Flux.from(findPublisher) .doOnNext(doc -> System.out.println(doc.toJson())) .blockLast();
{ "_id": 1, "color": "red", "qty": 5, "vendor": ["A"] } { "_id": 2, "color": "purple", "qty": 10, "vendor": ["C", "D"] } { "_id": 3, "color": "blue", "qty": 8, "vendor": ["B", "A"] } ...
逻辑
逻辑操作符根据指定方法的条件执行逻辑操作。
逻辑操作符方法包括:
逻辑方法 | matches |
|---|---|
具有所有筛选器条件的文档。此运算符使用逻辑 | |
具有任一筛选器条件的文档。此操作符使用逻辑 | |
不符合筛选条件的文档。 | |
无法匹配两个筛选器的文档。此运算符使用逻辑 |
以下示例创建一个筛选器,用于匹配 paint_purchases 集合中 qty 字段的值大于“8”或 color 字段的值等于“pink”的文档:
Bson orComparison = or(gt("qty", 8), eq("color", "pink")); FindPublisher<Document> findPublisher = collection.find(orComparison); Flux.from(findPublisher) .doOnNext(doc -> System.out.println(doc.toJson())) .blockLast();
{ "_id": 2, "color": "purple", "qty": 10, "vendor": ["C", "D"] } { "_id": 5, "color": "yellow", "qty": 11, "vendor": ["A", "B"] } { "_id": 6, "color": "pink", "qty": 5, "vendor": ["C"] }
数组
数组运算符会对文档中的数组字段求值。
数组操作符方法包括:
数组方法 | matches |
|---|---|
记录数组字段是否包含查询中指定的每个元素。 | |
如果数组字段中的元素与所有指定的条件均匹配,则选择文档。 | |
文档(如果数组字段是指定数量的元素)。 |
以下示例会将文档与 paint_purchases 集合中同时包含“A”和“D”的 vendor 数组进行匹配:
List<String> search = Arrays.asList("A", "D"); Bson allComparison = all("vendor", search); FindPublisher<Document> findPublisher = collection.find(allComparison); Flux.from(findPublisher) .doOnNext(doc -> System.out.println(doc.toJson())) .blockLast();
{ "_id": 8, "color": "orange", "qty": 7, "vendor": ["A", "D"] }
元素
元素运算符会计算指定字段的性质。
元素操作符方法包括:
以下示例匹配具有 qty字段且值不等于 paint_purchases集合中的 "5" 或 "8" 的文档:
Bson existsComparison = and(exists("qty"), nin("qty", 5, 8)); FindPublisher<Document> findPublisher = collection.find(existsComparison); Flux.from(findPublisher) .doOnNext(doc -> System.out.println(doc.toJson())) .blockLast();
{ "_id": 2, "color": "purple", "qty": 10, "vendor": ["C", "D"] } { "_id": 4, "color": "white", "qty": 6, "vendor": ["D"]} { "_id": 5, "color": "yellow", "qty": 11, "vendor": ["A", "B"] } { "_id": 8, "color": "orange", "qty": 7, "vendor": ["A", "D"] }
求值
评估操作符可评估一个文档中任何字段的值。
评估操作符方法包括:
评估方法 | matches |
|---|---|
对字段值进行模运算产生指定结果的文档。 | |
其值包含指定正则表达式的文档。 | |
包含指定全文搜索表达式的文档。 | |
包含指定 JavaScript 表达式的文档。 |
以下示例匹配 paint_purchases 集合中具有以字母“p”开头的 color 字段的文档:
Bson regexComparison = regex("color", "^p"); FindPublisher<Document> findPublisher = collection.find(regexComparison); Flux.from(findPublisher) .doOnNext(doc -> System.out.println(doc.toJson())) .blockLast();
{ "_id": 2, "color": "purple", "qty": 10, "vendor": ["C", "D"] } { "_id": 6, "color": "pink", "qty": 5, "vendor": ["C"] }
Bitwise
按位操作符将数字转换为其二进制值,以计算其位。
按位运算符方法包括:
按位法 | matches |
|---|---|
字段指定位已设置的文档(即为"1")。 | |
字段指定位已清除的文档(即为"0")。 | |
字段至少一个指定位已设置的文档(即为"1")。 | |
字段中至少有一个指定位已清除的文档(即“0”)。 |
以下示例匹配具有 a 字段的文档,该字段在 binary_numbers集合中相应位掩码“34”(即“00100010”)的位置设立了位:
Bson bitsComparison = bitsAllSet("a", 34); FindPublisher<Document> findPublisher = collection.find(bitsComparison); Flux.from(findPublisher) .doOnNext(doc -> System.out.println(doc.toJson())) .blockLast();
{ "_id": 9, "a": 54, "binaryValue": "00110110" } { "_id": 12, "a": 102, "binaryValue": "01100110" }
地理空间
地理空间操作符用于评估指定的坐标及其与形状或位置的关系。
地理空间操作符方法包括:
地理空间方法 | matches |
|---|---|
包含属于边界 GeoJSON 几何图形的 GeoJSON 几何图形值的文档。 | |
包含指定框中存在的坐标值的文档。 | |
包含指定多边形中存在的坐标值的文档。 | |
包含存在于指定圆形内的坐标值的文档。 | |
包含存在于指定圆形内的地理空间数据值(GeoJSON 或传统坐标对)的几何(使用球面几何)。 | |
与 GeoJSON 几何图形相交的几何图形。 | |
靠近某个点的地理空间对象。需要地理空间索引。 | |
靠近球面上某个点的地理空间对象。需要地理空间索引。 |
以下示例创建了一个过滤器,该过滤器匹配 point 字段包含 GeoJSON 几何图形的文档,而该几何图形属于 geo_points 集合中给定的 多边形:
Polygon square = new Polygon(Arrays.asList( new Position(0, 0), new Position(4, 0), new Position(4, 4), new Position(0, 4), new Position(0, 0))); Bson geoWithinComparison = geoWithin("coordinates", square); FindPublisher<Document> findPublisher = collection.find(geoWithinComparison); Flux.from(findPublisher) .doOnNext(doc -> System.out.println(doc.toJson())) .blockLast();
{ "_id": 13, "coordinates": {"type": "Point", "coordinates": [2.0, 2.0]} } { "_id": 15, "coordinates": {"type": "Point", "coordinates": [1.0, 3.0]} }