Docs 菜单
Docs 主页
/ / /
Scala 驱动程序
/

指定查询

在此页面上

  • Overview
  • 样本数据
  • 精确匹配
  • 比较操作符。
  • 逻辑操作符
  • 数组操作符
  • 元素操作符
  • 评估操作符
  • 更多信息
  • API 文档

在本指南中,您可以学习;了解如何使用Scala驾驶员指定查询。

您可以通过创建查询过滤来优化查询返回的文档设立。查询过滤是一个表达式,用于指定MongoDB在读取或写入操作中用于匹配文档的搜索条件。

您可以使用查询运算符在查询过滤器中Express更复杂的匹配条件。Scala驾驶员包含一个 Filters 类,该类为应用查询运算符提供辅助方法。

提示

要查看Filters 辅助方法的完整列表,请参阅 过滤器 API文档。

本指南中的示例对fruits集合运行操作,该集合包含表示水果的文档。 以下代码示例展示了如何创建数据库和集合,然后将示例文档插入集合:

val uri: String = "<connection string>"
val client: MongoClient = MongoClient(uri)
val database: MongoDatabase = client.getDatabase("db")
val collection: MongoCollection[Document] = database.getCollection("fruits")
// Inserts documents representing fruits
val fruits: Seq[Document] = Seq(
Document("_id" -> 1, "name" -> "apples", "qty" -> 5, "rating" -> 3, "color" -> "red", "type" -> Seq("fuji", "honeycrisp")),
Document("_id" -> 2, "name" -> "bananas", "qty" -> 7, "rating" -> 4, "color" -> "yellow", "type" -> Seq("cavendish")),
Document("_id" -> 3, "name" -> "oranges", "qty" -> 6, "rating" -> 2, "type" -> Seq("naval", "mandarin")),
Document("_id" -> 4, "name" -> "pineapples", "qty" -> 3, "rating" -> 5, "color" -> "yellow")
)
val result = collection.insertMany(fruits)
.subscribe((result: InsertManyResult) => println(result))

字面值查询返回与查询过滤完全匹配的文档。

以下示例将查询过滤指定为find()方法的参数。 此代码会返回color字段的值为"yellow"的所有文档:

val filter = equal("color", "yellow")
collection.find(filter).subscribe((doc: Document) => println(doc.toJson()),
(e: Throwable) => println(s"There was an error: $e"))
{"_id": 2, "name": "bananas", "qty": 7, "rating": 4, "color": "yellow", "type": ["cavendish"]}
{"_id": 4, "name": "pineapples", "qty": 3, "rating": 5, "color": "yellow"}

注意

查找所有文档

要查找集合中的所有文档,请调用 find() 方法而不传递任何参数:

collection.find()

比较操作符会根据查询过滤中的指定值评估文档字段值。 以下列表定义了常见的比较操作符及其相应的 Filters 辅助方法:

查询运算符
助手方法
说明

$gt

gt()

Matches documents in which the value of the given field is greater than the specified value.

$lte

lte()

Matches documents in which the value of the given field is less than or equal to the specified value.

$ne

ne()

Matches documents in which the value of the given field does not equal the specified value.

提示

要查看比较操作符的完整列表,请参阅 MongoDB Server 手册中的比较查询操作符指南。

以下示例将查询过滤传递给 find() 方法,并使用 gt() 方法应用$gt操作符。 此代码会返回 rating字段的值大于 2 的所有文档:

val filter = gt("rating", 2)
collection.find(filter).subscribe((doc: Document) => println(doc.toJson()),
(e: Throwable) => println(s"There was an error: $e"))
{"_id": 1, "name": "apples", "qty": 5, "rating": 3, "color": "red", "type": ["fuji", "honeycrisp"]}
{"_id": 2, "name": "bananas", "qty": 7, "rating": 4, "color": "yellow", "type": ["cavendish"]}
{"_id": 4, "name": "pineapples", "qty": 3, "rating": 5, "color": "yellow"}

逻辑操作符通过使用应用于两组或多组表达式结果的逻辑来匹配文档。 下表描述了每个逻辑操作符及其相应的 Filters 辅助方法:

查询运算符
助手方法
说明

$and

and()

Matches documents that satisfy the conditions of all clauses

$or

or()

Matches documents that satisfy the conditions of one clause

$nor

nor()

Matches documents that do not satisfy the conditions of any clause

$not

not()

Matches documents that do not match the expression

提示

要了解有关逻辑操作符的更多信息,请参阅 MongoDB Server 手册中的逻辑查询操作符指南。

以下示例将查询过滤传递给 find() 方法,并使用 or() 方法应用$or 逻辑操作符。 此代码会返回qty 字段值大于5 color字段值为"yellow" 的所有文档:

val filter = or(gt("qty", 5), equal("color", "yellow"))
collection.find(filter).subscribe((doc: Document) => println(doc.toJson()),
(e: Throwable) => println(s"There was an error: $e"))
{"_id": 2, "name": "bananas", "qty": 7, "rating": 4, "color": "yellow", "type": ["cavendish"]}
{"_id": 3, "name": "oranges", "qty": 6, "rating": 2, "type": ["naval", "mandarin"]}
{"_id": 4, "name": "pineapples", "qty": 3, "rating": 5, "color": "yellow"}

数组操作符根据大量字段中元素的值或数量来匹配文档。 下表描述了每个大量操作符及其相应的 Filters 辅助方法:

查询运算符
助手方法
说明

$all

all()

Matches documents that have arrays containing all elements in the query

$elemMatch

elemMatch()

Matches documents if an element in their array field satisfies all conditions in the query

$size

size()

Matches documents that have arrays of a specified size

提示

要了解有关数组操作符的更多信息,请参阅 MongoDB Server 手册中的数组查询操作符指南。

以下示例将查询过滤传递给 find() 方法,并使用 size() 方法应用$size大量操作符。 此代码会返回 type大量字段包含 2 元素的所有文档:

val filter = size("type", 2)
collection.find(filter).subscribe((doc: Document) => println(doc.toJson()),
(e: Throwable) => println(s"There was an error: $e"))
{"_id": 1, "name": "apples", "qty": 5, "rating": 3, "color": "red", "type": ["fuji", "honeycrisp"]}
{"_id": 3, "name": "oranges", "qty": 6, "rating": 2, "type": ["naval", "mandarin"]}

元素操作符根据字段的存在或类型查询数据。 下表描述了每个元素操作符及其相应的 Filters 辅助方法:

查询运算符
助手方法
说明

$exists

exists()

Matches documents that have the specified field

$type

type()

Matches documents if a field has the specified type

提示

要了解有关元素操作符的更多信息,请参阅 MongoDB Server 手册中的元素查询操作符指南。

以下示例将查询过滤传递给 find() 方法,并使用 exists() 方法应用$exists 元素操作符。 此代码会返回具有 color字段的所有文档:

val filter = exists("color")
collection.find(filter).subscribe((doc: Document) => println(doc.toJson()),
(e: Throwable) => println(s"There was an error: $e"))
{"_id": 1, "name": "apples", "qty": 5, "rating": 3, "color": "red", "type": ["fuji", "honeycrisp"]}
{"_id": 2, "name": "bananas", "qty": 7, "rating": 4, "color": "yellow", "type": ["cavendish"]}
{"_id": 4, "name": "pineapples", "qty": 3, "rating": 5, "color": "yellow"}

评估操作符根据单个字段或整个集合文档的评估返回数据。 下表描述了常见元素操作符及其相应的 Filters 辅助方法:

查询运算符
助手方法
说明

$text

text()

Performs a text search on documents

$regex

regex()

Matches documents that have values satisfying a specified regular expression

$mod

mod()

Performs a modulo operation on the value of a field and matches documents with a specified result

提示

要查看评估操作符的完整列表,请参阅 MongoDB Server 手册中的评估查询操作符指南。

以下示例将查询过滤传递给 find() 方法,并使用 regex() 方法应用$regex 评估操作符。 此代码使用正则表达式返回 name字段值至少包含两个连续 'p' 字符的所有文档:

val filter = regex("name", "p{2,}")
collection.find(filter).subscribe((doc: Document) => println(doc.toJson()),
(e: Throwable) => println(s"There was an error: $e"))
{"_id": 1, "name": "apples", "qty": 5, "rating": 3, "color": "red", "type": ["fuji", "honeycrisp"]}
{"_id": 4, "name": "pineapples", "qty": 3, "rating": 5, "color": "yellow"}

要了解有关查询文档的更多信息,请参阅 MongoDB Server 手册中的查询文档指南。

要学习;了解有关使用Scala驾驶员检索文档的更多信息,请参阅 检索数据指南。

要进一步了解本指南所讨论的任何方法或类型,请参阅以下 API 文档:

  • find()

  • 筛选器

  • insertMany()

后退

Retrieve Data