计算文档
Overview
在本指南中,您可以学习;了解如何使用Scala驾驶员检索集合中文档数量的准确估计数。 以下方法对集合中的文档进行计数:
countDocuments()
:返回与查询过滤匹配或存在于集合中的文档的确切数量estimatedDocumentCount()
:返回集合中的估计文档数
样本数据
本指南中的示例使用companies
sample_training
Atlas示例数据集的 数据库中的 集合。要从Scala应用程序访问权限此集合,请创建一个连接到Atlas 集群的MongoClient
,并将以下值分配给 database
和 collection
变量:
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
类的一些成员函数,您可以使用这些函数为计数操作设立选项:
方法 | 说明 |
---|---|
| Sets the collation to use for the operation. Parameter Type: Collation |
| Sets the index to use for the operation. ParameterType: Bson |
| Sets the maximum number of documents to count. This value must be a positive integer. Parameter Type: int |
| Sets the maximum amount of time that the operation can run. Parameter Types: long and TimeUnit |
| 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 文档
要进一步了解本指南所讨论的任何方法或类型,请参阅以下 API 文档: