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

指定查询

在本页

  • 概述
  • 比较运算符
  • 逻辑运算符
  • 数组运算符
  • 元素运算符
  • 评估运算符

在本指南中,您可以了解如何在MongoDB Java驱动程序中指定查询。

大多数CRUD操作都允许您通过在查询过滤器中指定匹配条件来缩小匹配文档的集合。查询过滤器包含一个或多个应用于特定字段的查询运算符,这些字段决定了哪些文档应包含在结果集中。

在本页中,我们将介绍以下查询运算符,并提供如何使用它们的示例

  • 比较运算符

  • 逻辑运算符

  • 数组运算符

  • 元素运算符

  • 评估运算符

本指南中的示例使用以下文档在paint_purchases 集合中

{ "_id": 1, "color": "red", "qty": 9, "vendor": ["A", "E"] }
{ "_id": 2, "color": "purple", "qty": 8, "vendor": ["B", "D", "F"], "rating": 5 }
{ "_id": 3, "color": "blue", "qty": 5, "vendor": ["A", "E"] }
{ "_id": 4, "color": "white", "qty": 6, "vendor": ["D"], "rating": 9 }
{ "_id": 5, "color": "yellow", "qty": 4, "vendor": ["A", "B"] }
{ "_id": 6, "color": "pink", "qty": 3, "vendor": ["C"] }
{ "_id": 7, "color": "green", "qty": 8, "vendor": ["C", "E"], "rating": 7 }
{ "_id": 8, "color": "black", "qty": 7, "vendor": ["A", "C", "D"] }

比较运算符根据与集合中值的比较来查询数据。常见的比较运算符包括 gt() 用于“大于”比较,lte() 用于“小于或等于”比较,以及 ne() 用于“不等于”比较。

以下示例使用 Filters.gt() 方法匹配所有在 paint_purchases 集合中 qty 的值大于 7 的文档。

Bson filter = Filters.gt("qty", 7);
// Retrieves documents that match the filter and prints them as JSON
collection.find(filter).forEach(doc -> System.out.println(doc.toJson()));

以下显示了上述查询的输出

{ "_id": 1, "color": "red", "qty": 9, "vendor": ["A", "E"] }
{ "_id": 2, "color": "purple", "qty": 8, "vendor": ["B", "D", "F"], "rating": 5 }
{ "_id": 7, "color": "green", "qty": 8, "vendor": ["C", "E"], "rating": 7 }

逻辑运算符通过应用字段级运算符的结果来查询数据。常见的逻辑运算符包括and(),其中所有运算符都必须为真,以及or(),其中至少有一个运算符必须为真。

以下示例使用Filters.and()方法匹配在paint_purchases集合中qty的值小于或等于5并且color的值不是"pink"的文档。

Bson filter = Filters.and(Filters.lte("qty", 5), Filters.ne("color", "pink"));
// Retrieves documents that match the filter and prints them as JSON
collection.find(filter).forEach(doc -> System.out.println(doc.toJson()));

以下显示了上述查询的输出

{ "_id": 3, "color": "blue", "qty": 5, "vendor": ["A", "E"] }
{ "_id": 5, "color": "yellow", "qty": 4, "vendor": ["A", "B"] }

数组运算符基于数组字段中元素的值或数量来查询数据。

以下示例使用Filters.size()方法匹配在paint_purchases集合中vendor列表的大小为3的文档。

Bson filter = Filters.size("vendor", 3);
// Retrieves documents that match the filter and prints them as JSON
collection.find(filter).forEach(doc -> System.out.println(doc.toJson()));

以下显示了上述查询的输出

{ "_id": 2, "color": "purple", "qty": 8, "vendor": ["B", "D", "F"], "rating": 5 }
{ "_id": 8, "color": "black", "qty": 7, "vendor": ["A", "C", "D"] }

元素操作符基于字段的存在的或类型查询数据。

以下示例使用 Filters.exists() 方法匹配具有 ratingpaint_purchases 集合中的文档

Bson filter = Filters.exists("rating");
// Retrieves documents that match the filter and prints them as JSON
collection.find(filter).forEach(doc -> System.out.println(doc.toJson()));

以下显示了上述查询的输出

{ "_id": 2, "color": "purple", "qty": 8, "vendor": ["B", "D", "F"], "rating": 5 }
{ "_id": 4, "color": "white", "qty": 6, "vendor": ["D"], "rating": 9 }
{ "_id": 7, "color": "green", "qty": 8, "vendor": ["C", "E"], "rating": 7 }

评估操作符查询基于高级逻辑的数据,如正则表达式和文本搜索。

以下示例使用 Filters.regex() 方法匹配在 paint_purchases 集合中具有以字母 "k" 结尾的 color 的文档

Bson filter = Filters.regex("color", "k$");
// Retrieves documents that match the filter and prints them as JSON
collection.find(filter).forEach(doc -> System.out.println(doc.toJson()));

以下显示了上述查询的输出

{ "_id": 6, "color": "pink", "qty": 3, "vendor": ["C"] }
{ "_id": 8, "color": "black", "qty": 7, "vendor": ["A", "C", "D"] }

有关本指南中提到的操作符的更多信息,请参阅以下服务器手册条目

返回

批量操作