Join us at MongoDB.local London on 7 May to unlock new possibilities for your data. Use WEB50 to save 50%.
Register now >
Docs 菜单
Docs 主页
/ /

计算文档

在本指南中,您可以学习;了解如何使用Rust驾驶员检索集合中文档数量的准确估计数。 count_documents() 方法返回与查询过滤匹配或存在于集合中的文档的确切数量,estimated_document_count() 方法返回集合中文档的估计数量。

本指南中的示例使用restaurants sample_restaurantsAtlas示例数据集的 数据库中的 集合。要从Rust应用程序访问权限此集合,请创建一个连接到AtlasClient 集群的 ,并将以下值分配给my_coll 变量。

选择 AsynchronousSynchronous标签页,查看每个运行时的相应代码:

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 集合中文档的模型:

#[derive(Serialize, Deserialize, Debug)]
struct Restaurant {
name: String,
cuisine: String,
}

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

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

要返回集合中所有文档的计数,请将空过滤文档传递给 count_documents() 方法,如以下示例所示。选择 AsynchronousSynchronous标签页,查看每个运行时的相应代码:

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" 的文档数量。选择 AsynchronousSynchronous标签页,查看每个运行时的相应代码:

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() 方法的行为,方法是在 awaitrun() 方法调用之前将选项方法链接到 count_documents() 调用。下表描述了可设立自定义计数操作的选项:

选项
说明

collation()

The collation to use for the operation.
Type: Collation

hint()

The index to use for the operation.
Type: Hint

comment()

The comment to attach to the operation.
Type: Bson

limit()

The maximum number of documents to count. This value must be a positive integer.
Type: u64

max_time()

The maximum amount of time in milliseconds that the operation can run.
Type: Duration

read_concern()

The read concern to use for the operation.
Type: ReadConcern

skip()

The number of documents to skip before counting documents.
Type: u64

selection_criteria()

The read preference and tags to use for the operation.
Type: SelectionCriteria

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

以下示例估计集合中的文档数。选择 AsynchronousSynchronous标签页,查看每个运行时的相应代码:

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() 方法的行为。下表描述了可用于自设立估计计数操作的选项:

选项
说明

max_time()

The maximum amount of time in milliseconds that the operation can run.
Type: Duration

comment()

The comment to attach to the operation.
Type: Bson

read_concern()

The read concern to use for the operation.
Type: ReadConcern

selection_criteria()

The read preference and tags to use for the operation.
Type: SelectionCriteria

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

后退

指定要返回的字段

在此页面上