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

Retrieve Data

在此页面上

  • Overview
  • 样本数据
  • 查找文档
  • 查找多个文档
  • 查找一个文档
  • 修改查找行为
  • 更多信息
  • API 文档

在本指南中,您可以学习;了解如何使用Scala驾驶员通过读取操作从MongoDB集合中检索数据。您可以对集合调用 find() 方法,以检索与一设立条件匹配的文档。

本指南中的示例使用companies sample_trainingAtlas示例数据集的 数据库中的 集合。要从Scala应用程序访问权限此集合,请创建一个连接到Atlas 集群的MongoClient,并将以下值分配给 databasecollection 变量:

val database: MongoDatabase = mongoClient.getDatabase("sample_training")
val collection: MongoCollection[Document] = database.getCollection("companies")

要学习;了解如何创建免费的MongoDB Atlas 群集并加载示例数据集,请参阅Atlas入门指南。

要从集合中检索文档,请使用 find() 方法。 此方法接受查询过滤参数并返回 FindObservable类的实例,您可以从该实例访问权限查询结果。FindObservable 类还提供了其他方法,您可以将这些方法链接到 FindObservable实例以修改其行为,例如 first()

提示

要学习;了解有关查询筛选器的更多信息,请参阅《 指定查询》指南。

要查找集合中的多个文档,请将查询筛选器传递给 find() 方法,其中指定要检索的文档条件。

find() 方法返回一个 FindObservable实例,您可以遍历该实例以查看匹配的文档。 使用 subscribe() 方法遍历 FindObservable

以下示例使用 find() 方法查找 founded_year字段值为 1970 的所有文档并打印结果:

val filter = equal("founded_year", 1970)
collection.find(filter).subscribe((doc: Document) => println(doc.toJson()),
(e: Throwable) => println(s"There was an error: $e"))
{"_id":{"$oid":"..."},"name":"Mitsubishi Motors","permalink":"mitsubishi-motors",
"crunchbase_url":"http://www.crunchbase.com/company/mitsubishi-motors",
... }
{"_id":{"$oid":"..."},"name":"Western Digital","permalink":"western-digital",
"crunchbase_url":"http://www.crunchbase.com/company/western-digital",
... }
{"_id":{"$oid":"..."},"name":"Celarayn","permalink":"celarayn","crunchbase_url":
"http://www.crunchbase.com/company/celarayn",
... }

注意

查找所有文档

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

collection.find()

要查找集合中的单个文档,请调用 find() 方法并传递查询过滤,其中指定要查找的文档条件。 然后,将 first() 方法链接到 find()

find() 方法返回一个 FindObservable实例,first() 方法返回一个 SingleObserver实例,其中包含 FindObservable 存储的第一个查询结果。 您可以通过调用 subscribe() 方法访问权限SingleObserver 结果。

以下示例使用 find()first() 方法查找 name字段值为 "LinkedIn" 的第一个文档:

val filter = equal("name", "LinkedIn")
collection.find(filter).first().subscribe((doc: Document) => println(doc.toJson()),
(e: Throwable) => println(s"There was an error: $e"))
{"_id": {"$oid": "..."}, "name": "LinkedIn", "permalink": "linkedin", "crunchbase_url":
"http://www.crunchbase.com/company/linkedin", "homepage_url": "http://linkedin.com",
...}

提示

排序顺序

如果未指定排序条件,first() 方法将按自然顺序返回磁盘上的第一份文档。

您可以通过链接 FindObservable 类提供的方法来修改 find() 方法的行为。 下表描述了其中一些方法:

方法
说明

explain()

Explains the execution plan for this operation with the specified verbosity level.
Parameter Type: ExplainVerbosity

collation()

Sets the collation to use for the operation. The default value is the collation specified for the collection.
Parameter Type: Collation

comment()

Attaches a comment to the operation.
Parameter Type: String

first()

Returns an Observable that stores only the first query result. To view an example that uses this method, see Find One Document on this page.

limit()

Sets the maximum number of documents the operation can return.
Parameter Type: Int

skip()

Sets the number of documents to skip before returning results.
Parameter Type: Int

sort()

Sets the order in which the operation returns matching documents.
Parameter Type: Bson

以下示例使用 find() 方法查找 number_of_employees字段值为 1000 的所有文档。 该示例使用 limit() 方法返回最多 5 个结果:

val filter = equal("number_of_employees", 1000)
collection.find(filter).limit(5).subscribe((doc: Document) => println(doc.toJson()),
(e: Throwable) => println(s"There was an error: $e"))
{"_id": {"$oid": "..."}, "name": "Akamai Technologies", "permalink": "akamai-technologies",
"crunchbase_url": "http://www.crunchbase.com/company/akamai-technologies", "homepage_url":
"http://www.akamai.com", ... }
{"_id": {"$oid": "..."}, "name": "Yodle", "permalink": "yodle", "crunchbase_url":
"http://www.crunchbase.com/company/yodle", "homepage_url": "http://www.yodle.com", ... }
{"_id": {"$oid": "..."}, "name": "Antal International", "permalink": "antal-international",
"crunchbase_url": "http://www.crunchbase.com/company/antal-international", "homepage_url":
"http://antal.com", ... }
{"_id": {"$oid": "..."}, "name": "Yatra online", "permalink": "yatra-online", "crunchbase_url":
"http://www.crunchbase.com/company/yatra-online", "homepage_url": "http://www.Yatra.com", ... }
{"_id": {"$oid": "..."}, "name": "Gumtree", "permalink": "gumtree", "crunchbase_url":
"http://www.crunchbase.com/company/gumtree", "homepage_url": "http://www.gumtree.co.za", ... }

有关FindObservable 成员方法的完整列表,请参阅 FindObservable 类的API文档。

如需学习;了解有关查询筛选器的更多信息,请参阅“指定查询”指南。

要查看使用Scala驾驶员检索文档的代码示例,请参阅读取数据。

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

  • find()

  • first()

  • limit()

后退

读取数据