Docs 菜单
Docs 主页
/ / /
Scala

从 MongoDB 读取数据

在此页面上

  • Overview
  • 示例应用程序
  • 找到一个
  • 查找多个
  • 对集合中的文档进行计数
  • 对查询返回的文档进行计数
  • 估计文档计数
  • Retrieve Distinct Values
  • 监控数据变化

在此页面上,您可以查看可复制的代码示例,这些示例显示了可用于从MongoDB读取数据的常见Scala驾驶员方法。

提示

要了解有关此页面上显示的任何方法的更多信息,请参阅每个部分中提供的链接。

要使用本页中的示例,请将代码示例复制到示例应用程序或您自己的应用程序中。 请务必将代码示例中的所有占位符(例如 <connection string URI> )替换为 MongoDB 部署的相关值。

您可以使用以下示例应用程序来测试本页上的代码示例。 要使用示例应用程序,请执行以下步骤:

  1. 确保您已在 Maven 或 sbt项目中安装Scala驾驶员。

  2. 复制以下代码并将其粘贴到新的.scala文件中。

  3. 从此页面复制代码示例,并将其粘贴到文件中的指定行。

1import org.mongodb.scala._
2import org.mongodb.scala.bson.Document
3import org.mongodb.scala.model.Filters._
4import org.mongodb.scala.model.changestream._
5
6object SampleReadApp {
7
8 def main(args: Array[String]): Unit = {
9 val mongoClient = MongoClient("<connection string URI>")
10 val database: MongoDatabase = mongoClient.getDatabase("<database name>")
11 val collection: MongoCollection[Document] = database.getCollection("<collection name>")
12
13
14 // Start example code here
15
16 // End example code here
17
18 // Wait for the operations to complete before closing client
19 // Note: This example uses Thread.sleep() for brevity and does not guarantee all
20 // operations will be completed in time
21 Thread.sleep(1000)
22 mongoClient.close()
23 }
24}

以下示例检索与给定过滤指定的条件相匹配的文档:

val filter = equal("<field>", "<value>")
collection.find(filter).first().subscribe((doc: Document) => println(doc.toJson()),
(e: Throwable) => println(s"There was an error: $e"))

要学习;了解有关first() 方法的详情,请参阅 检索数据指南。

以下示例检索与给定过滤指定的条件匹配的所有文档:

val filter = equal("<field>", "<value>")
collection.find(filter).subscribe((doc: Document) => println(doc.toJson()),
(e: Throwable) => println(s"There was an error: $e"))

要了解有关find()方法的更多信息,请参阅检索数据指南。

以下示例返回指定集合中的文档数:

collection.countDocuments()
.subscribe((count: Long) => println(s"Number of documents: $count"),
(e: Throwable) => println(s"There was an error: $e"))

要学习;了解有关countDocuments()方法的更多信息,请参阅文档计数指南。

以下示例返回指定集合中与查询条件匹配的文档数:

collection.countDocuments(equal("<field>", "<value>"))
.subscribe((count: Long) => println(s"Number of documents: $count"),
(e: Throwable) => println(s"There was an error: $e"))

要学习;了解有关countDocuments()方法的更多信息,请参阅文档计数指南。

以下示例根据集合元数据返回指定集合中文档的大致数量:

collection.estimatedDocumentCount()
.subscribe((count: Long) => println(s"Estimated number of documents: $count"),
(e: Throwable) => println(s"There was an error: $e"))

要学习;了解有关estimatedDocumentCount()方法的更多信息,请参阅文档计数指南。

以下示例返回给定集合中指定字段名称的所有非重复值:

collection.distinct("<field>")
.subscribe((value: String) => println(value),
(e: Throwable) => println(s"There was an error: $e"))

要了解有关distinct()方法的更多信息,请参阅“检索不同字段值”指南。

以下示例为给定集合创建变更流,并打印该集合中的后续变更事件:

val changeStreamObservable = collection.watch()
changeStreamObservable.subscribe(
(changeEvent: ChangeStreamDocument[Document]) => {
println(s"Received a change to the collection: ${changeEvent}")
},
(e: Throwable) => {
println(s"Encountered an error: ${e.getMessage}")
},
() => println("Completed")
)

要学习;了解有关watch()方法的更多信息,请参阅《监控数据更改》指南。

后退

时间序列