文档菜单
文档首页
/ / /
Java 同步驱动程序
/ /

过滤器构建器

本页内容

  • 概述
  • 比较
  • 逻辑
  • 数组
  • 元素
  • 评估
  • 位运算
  • 地理空间

在本指南中,您可以学习如何使用 构建器 在 MongoDB Java 驱动程序中指定查询的 过滤器

构建器是由 MongoDB Java 驱动程序提供的类,可以帮助您构建BSON 对象。想了解更多信息,请参阅我们的构建器指南。

过滤器是 MongoDB 用于限制您看到的结果的操作。

使用过滤器的一些地方包括

  • find()

  • 聚合的 match 阶段

  • deleteOne()/deleteMany()

  • updateOne()/updateMany()

具有过滤器查询的结果的一些示例包括

  • 价格在 $0 到 $25 之间的物品

  • 提供室内游泳池和免费停车的酒店

  • 提到“辛辣”的美食评论

本指南通过以下类型操作符的示例展示了如何使用构建器

  • 比较

  • 逻辑

  • 数组

  • 元素

  • 评估

  • 位运算

  • 地理空间

Filters 类提供了所有 MongoDB 查询操作符的静态工厂方法。每个方法都返回一个 BSON 类型的实例,您可以将它传递给任何期望查询过滤器的任何方法。

提示

为了简洁起见,您可以选择静态导入 Filters 类的方法。

import static com.mongodb.client.model.Filters.*;

以下示例假设已进行此静态导入。

本指南中的 Filter 示例使用以下样本集合

集合: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] } }

比较过滤器包括所有将文档中的值与指定值进行比较的操作符。

比较操作符方法包括

比较方法
匹配
值等于指定值。
值大于指定值。
值大于或等于指定值。
值小于指定值。
值小于或等于指定值。
值不等于指定值。
数组中指定的任何值。
数组中未指定的任何值。
所有文档。

以下示例创建了一个匹配所有在 paint_purchases 集合中 qty 字段值为 "5" 的文档的过滤器

Bson equalComparison = eq("qty", 5);
collection.find(equalComparison).forEach(doc -> System.out.println(doc.toJson()));

以下显示了先前查询的输出

{ "_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);
collection.find(gteComparison).forEach(doc -> System.out.println(doc.toJson()));

以下显示了先前查询的输出

{ "_id": 2, "color": "purple", "qty": 10, "vendor": ["C", "D"] }
{ "_id": 5, "color": "yellow", "qty": 11, "vendor": ["A", "B"] }

以下示例创建了一个匹配所有在 paint_purchases 集合的过滤器,因为谓词为空

Bson emptyComparison = empty();
collection.find(emptyComparison).forEach(doc -> System.out.println(doc.toJson()));

先前查询的输出包含集合中的所有文档。

{ "_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"] }
...

逻辑运算符根据指定方法的条件执行逻辑操作。

逻辑运算符方法包括

逻辑方法
匹配
具有所有过滤器条件的文档。此运算符使用逻辑AND连接过滤器。
具有任一过滤器条件的文档。此运算符使用逻辑OR连接过滤器。
不匹配过滤器条件的文档。
同时不匹配两个过滤器的文档。此运算符使用逻辑NOR连接过滤器。

以下示例创建了一个过滤器,该过滤器匹配在

paint_purchases

集合中,qty字段的值大于"8"或color字段的值等于"pink"的文档。

Bson orComparison = or(gt("qty", 8), eq("color", "pink"));
collection.find(orComparison).forEach(doc -> System.out.println(doc.toJson()));

以下显示了先前查询的输出

{ "_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"] }

数组运算符评估文档中的数组字段。

数组运算符方法包括

数组方法
匹配
如果数组字段包含查询中指定的每个元素,则匹配文档。
如果数组字段中的元素匹配所有指定的条件,则匹配文档。
如果数组字段是指定数量的元素,则匹配文档。

以下示例匹配在

paint_purchases

集合中,vendors数组同时包含"A"和"D"的文档。

List<String> search = Arrays.asList("A", "D");
Bson allComparison = all("vendor", search);
collection.find(allComparison).forEach(doc -> System.out.println(doc.toJson()));

以下显示了先前查询的输出

{ "_id": 8, "color": "orange", "qty": 7, "vendor": ["A", "D"] }

元素运算符评估指定字段的性质。

元素运算符方法包括

元素方法
匹配
具有指定字段的文档。
字段为指定类型的文档。

以下示例匹配具有 qty 字段且其值不等于 "5" 或 "8" 的 paint_purchases 集合中的文档

Bson existsComparison = and(exists("qty"), nin("qty", 5, 8));
collection.find(existsComparison).forEach(doc -> System.out.println(doc.toJson()));

以下显示了先前查询的输出

{ "_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"] }

评估运算符评估文档中任何字段的值。

评估运算符方法包括

评估方法
匹配
字段值进行取模运算后产生指定结果的文档。
值包含指定正则表达式的文档。
包含指定全文搜索表达式的文档。
包含指定JavaScript表达式的文档。

以下示例匹配在 paint_purchases 集合中具有以字母 "p" 开头的 color 字段的文档

Bson regexComparison = regex("color", "^p");
collection.find(regexComparison).forEach(doc -> System.out.println(doc.toJson()));

以下显示了先前查询的输出

{ "_id": 2, "color": "purple", "qty": 10, "vendor": ["C", "D"] }
{ "_id": 6, "color": "pink", "qty": 5, "vendor": ["C"] }

位运算符将数字转换为二进制值以评估其位。

位运算符方法包括

位运算方法
匹配
字段中指定的位被设置(即“1”)的文档。
字段中指定的位被清除(即“0”)的文档。
字段中至少有一个指定的位被设置(即“1”)的文档。
字段中至少有一个指定的位被清除(即“0”)的文档。

以下示例匹配具有在 bitField 字段中设置位的位置对应掩码“34”(即“00100010”)的 binary_numbers 集合中的文档

Bson bitsComparison = bitsAllSet("a", 34);
collection.find(bitsComparison).forEach(doc -> System.out.println(doc.toJson()));

以下显示了先前查询的输出

{ "_id": 9, "a": 54, "binaryValue": "00110110" }
{ "_id": 12, "a": 102, "binaryValue": "01100110" }

地理空间运算符评估指定坐标及其与形状或位置的关系。

地理空间运算符方法包括

地理空间方法
匹配
包含落在边界GeoJSON几何形状内的GeoJSON几何值的文档。
包含存在于指定框内的坐标值的文档。
包含存在于指定多边形内的坐标值的文档。
包含存在于指定圆内的坐标值的文档。
包含存在于指定圆内且使用球面几何的地理空间数据值(GeoJSON或旧坐标对)的几何形状。
与GeoJSON几何形状相交的几何形状。支持 $geoIntersects2dsphere 索引。
与点临近的地理空间对象。需要地理空间索引。2dsphere2d 索引支持 $near
球面上与点临近的地理空间对象。需要地理空间索引。2dsphere2d 索引支持 $nearSphere

以下示例创建了一个过滤器,匹配 point 字段包含在 geo_points 集合中给定 多边形 内的 GeoJSON 几何形状的文档

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)));
// Prints documents that contain "coordinates" values that are within the bounds of the polygon passed as the filter parameter
Bson geoWithinComparison = geoWithin("coordinates", square);
collection.find(geoWithinComparison).forEach(doc -> System.out.println(doc.toJson()));

以下显示了先前查询的输出

{ "_id": 13, "coordinates": {"type": "Point", "coordinates": [2.0, 2.0]} }
{ "_id": 15, "coordinates": {"type": "Point", "coordinates": [1.0, 3.0]} }

返回

聚合