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

计算文档

在此页面上

  • Overview
  • 样本数据
  • 检索准确的计数
  • 对所有文档进行计数
  • 对特定文档进行计数
  • 自定义计数行为
  • 检索估计计数
  • 自定义估计计数行为
  • API 文档

在本指南中,您可以学习;了解如何使用Scala驾驶员检索集合中文档数量的准确估计数。 以下方法对集合中的文档进行计数:

  • countDocuments():返回与查询过滤匹配或存在于集合中的文档的确切数量

  • estimatedDocumentCount():返回集合中的估计文档数

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

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

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

使用countDocuments()方法计算集合中的文档数量。 要计算匹配特定搜索条件的文档数量,请将查询过滤传递给countDocuments()方法。

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

要返回集合中所有文档的计数,请调用 countDocuments() 方法而不传递任何参数,如以下示例所示:

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

要返回匹配特定搜索条件的文档计数,请将查询过滤传递给countDocuments()方法。

以下示例计算founded_year字段的值为2010的文档数量:

collection.countDocuments(equal("founded_year", 2010))
.subscribe((count: Long) => println(s"Companies founded in 2010: $count"),
(e: Throwable) => println(s"There was an error: $e"))
Number of companies founded in 2010: 33

您可以通过将 CountOptions实例作为参数传递来修改 countDocuments() 方法的行为。 下表描述了 CountOptions 类的一些成员函数,您可以使用这些函数为计数操作设立选项:

方法
说明

collation()

Sets the collation to use for the operation.
Parameter Type: Collation

hint()

Sets the index to use for the operation.
ParameterType: Bson

limit()

Sets the maximum number of documents to count. This value must be a positive integer.
Parameter Type: int

maxTime()

Sets the maximum amount of time that the operation can run.
Parameter Types: long and TimeUnit

skip()

Sets the number of documents to skip before counting documents.
Parameter Type: int

以下示例使用countDocuments()方法计算number_of_employees字段值为50的文档数量,并指示操作最多计算100结果:

val countOptions = CountOptions().limit(100)
collection.countDocuments(equal("number_of_employees", 50), countOptions)
.subscribe((count: Long) => println(s"Companies with 50 employees: $count"),
(e: Throwable) => println(s"There was an error: $e"))
Number of companies with 50 employees: 100

您可以通过调用estimatedDocumentCount()方法来检索集合中文档数量的估计值。 该方法根据集合元数据估计文档数量,这可能比执行精确计数更快。

以下示例估计集合中的文档数量:

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

您可以通过将 EstimatedDocumentCountOptions实例作为参数传递来修改 estimatedDocumentCount() 方法的行为。 EstimatedDocumentCountOptions 类包括 maxTime() 成员函数,您可以使用该函数配置 EstimatedDocumentCountOptions对象并设立计数操作可以运行的最长时间。

以下示例使用 estimatedDocumentCount() 方法返回集合中文档数量的估计值,并将操作超时设置为 3 秒:

val estimatedOptions = EstimatedDocumentCountOptions().maxTime(3, SECONDS)
collection.estimatedDocumentCount(estimatedOptions)
.subscribe((count: Long) => println(s"Estimated number of documents: $count"),
(e: Throwable) => println(s"There was an error: $e"))
Estimated number of documents: 9500

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

后退

非重复字段值