Overview
在本指南中,您可以学习;了解如何使用Rust驾驶员检索集合中文档数量的准确估计数。 count_documents() 方法返回与查询过滤匹配或存在于集合中的文档的确切数量,estimated_document_count() 方法返回集合中文档的估计数量。
样本数据
本指南中的示例使用restaurants sample_restaurantsAtlas示例数据集的 数据库中的 集合。要从Rust应用程序访问权限此集合,请创建一个连接到AtlasClient 集群的 ,并将以下值分配给my_coll 变量。
选择 Asynchronous或Synchronous标签页,查看每个运行时的相应代码:
let uri = "<connection string>"; let client = Client::with_uri_str(uri).await?; let my_coll: Collection<Document> = client .database("sample_restaurants") .collection("restaurants");
let uri = "<connection string>"; let client = Client::with_uri_str(uri)?; let my_coll: Collection<Document> = client .database("sample_restaurants") .collection("restaurants");
要学习;了解如何创建免费的MongoDB Atlas 群集并加载示例数据集,请参阅 Rust驱动程序入门指南。
本指南中的示例使用以下 Restaurant 结构作为 restaurants 集合中文档的模型:
struct Restaurant { name: String, cuisine: String, }
检索准确的计数
使用 count_documents() 方法计算集合中的文档数量。要计算符合特定搜索条件的文档数量,请将查询过滤文档传递给 count_documents() 方法。
要了解有关指定查询的更多信息,请参阅指定查询指南。
对所有文档进行计数
要返回集合中所有文档的计数,请将空过滤文档传递给 count_documents() 方法,如以下示例所示。选择 Asynchronous 或 Synchronous标签页,查看每个运行时的相应代码:
let ct = my_coll.count_documents().await?; println!("Number of matching documents: {}", ct);
Number of documents: 25216
let ct = my_coll.count_documents().run()?; println!("Number of matching documents: {}", ct);
Number of documents: 25216
对特定文档进行计数
要返回匹配特定搜索条件的文档计数,请将查询过滤文档传递给count_documents()方法。
以下示例计算 name字段的值包含字符串 "Sunset" 的文档数量。选择 Asynchronous 或 Synchronous标签页,查看每个运行时的相应代码:
let ct = my_coll .count_documents(doc! { "name": doc! { "$regex": "Sunset" } }) .await?; println!("Number of matching documents: {}", ct);
Number of matching documents: 10
let ct = my_coll .count_documents(doc! { "name": doc! { "$regex": "Sunset" } }) .run()?; println!("Number of matching documents: {}", ct);
Number of matching documents: 10
自定义计数行为
您可以修改 count_documents() 方法的行为,方法是在 await 或 run() 方法调用之前将选项方法链接到 count_documents() 调用。下表描述了可设立自定义计数操作的选项:
选项 | 说明 |
|---|---|
| The collation to use for the operation. Type: Collation |
| The index to use for the operation. Type: Hint |
| The comment to attach to the operation. Type: Bson |
| The maximum number of documents to count. This value must be a positive integer. Type: u64 |
| The maximum amount of time in milliseconds that the operation can run. Type: Duration |
| The read concern to use for the operation. Type: ReadConcern |
| The number of documents to skip before counting documents. Type: u64 |
| The read preference and tags to use for the operation. Type: SelectionCriteria |
检索估计计数
您可以通过在调用 await 或 run() 方法之前调用 estimated_document_count() 方法来检索集合中文档数量的检索值。该方法根据集合元数据估计文档数量,这可能比执行精确计数更快。
以下示例估计集合中的文档数。选择 Asynchronous 或 Synchronous标签页,查看每个运行时的相应代码:
let ct = my_coll.estimated_document_count().await?; println!("Number of documents: {}", ct);
Number of documents: 25216
let ct = my_coll.estimated_document_count().run()?; println!("Number of documents: {}", ct);
Number of documents: 25216
自定义估计计数行为
您可以通过将选项方法链接到 estimated_document_count() 调用来修改 estimated_document_count() 方法的行为。下表描述了可用于自设立估计计数操作的选项:
选项 | 说明 |
|---|---|
| The maximum amount of time in milliseconds that the operation can run. Type: Duration |
| The comment to attach to the operation. Type: Bson |
| The read concern to use for the operation. Type: ReadConcern |
| The read preference and tags to use for the operation. Type: SelectionCriteria |
API 文档
要进一步了解本指南所讨论的任何方法或类型,请参阅以下 API 文档: