对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Make the MongoDB docs better! We value your opinion. Share your feedback for a chance to win $100.
MongoDB Branding Shape
Click here >
Docs 菜单

计算文档

在本指南中,您可以学习如何使用C++驱动程序检索集合中文档数量的准确估计数。count_documents() 方法返回与查询筛选条件匹配或存在于集合中的文档的确切数量,estimated_document_count() 方法返回集合中文档的估计数量。

本指南中的示例使用Atlas示例数据集sample_training数据库中的companies集合。 要从C++应用程序访问权限此集合,请实例化一个连接到Atlas 集群的mongocxx::client ,并将以下值分配给dbcollection变量:

auto db = client["sample_training"];
auto collection = db["companies"];

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

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

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

要返回集合中所有文档的计数,请将空过滤文档传递给count_documents()方法,如以下示例所示:

auto result = collection.count_documents({});
std::cout << "Number of documents: " << result << std::endl;
Number of documents: 9500

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

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

auto result = collection.count_documents(make_document(kvp("founded_year", 2010)));
std::cout << "Number of companies founded in 2010: " << result << std::endl;
Number of companies founded in 2010: 33

您可以通过将mongocxx::options::count类的实例作为参数传递来修改count_documents()方法的行为。 下表描述了您可以在mongocxx::options::count实例中设立的字段:

字段
说明

collation

用于操作的排序规则。类型:
bsoncxx::document::view_or_value

hint

用于操作的索引。类型:
mongocxx::hint

comment


要附加到操作的注释。类型:bsoncxx::types::bson_value::view_or_value

limit

要计数的最大文档数。此值必须是正整数。类型:
std::int64_t

max_time


操作可以运行的最长时间(以毫秒为单位)。类型:std::chrono::milliseconds

skip

在对文档进行计数之前要跳过的文档数。类型:
std::int64_t

read_preference

用于该操作的读取偏好(read
preference)。类型:mongocxx::read_preference

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

mongocxx::options::count opts;
opts.limit(100);
auto result = collection.count_documents(make_document(kvp("number_of_employees", 50)), opts);
std::cout << "Number of companies with 50 employees: " << result << std::endl;
Number of companies with 50 employees: 100

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

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

auto result = collection.estimated_document_count();
std::cout << "Estimated number of documents: " << result << std::endl;
Estimated number of documents: 9500

您可以通过将mongocxx::options::estimated_document_count类的实例作为参数传递来修改estimated_document_count()方法的行为。 下表描述了您可以在mongocxx::options::estimated_document_count实例中设立的字段:

字段
说明

max_time


操作可以运行的最长时间(以毫秒为单位)。类型:std::chrono::milliseconds

comment


要附加到操作的注释。类型:bsoncxx::types::bson_value::view_or_value

read_preference

用于该操作的读取偏好(read
preference)。类型:mongocxx::read_preference

以下示例使用estimated_document_count()方法返回集合中文档的估计数量,并指示操作最多运行1000毫秒:

mongocxx::options::estimated_document_count opts;
opts.max_time(std::chrono::milliseconds{1000});
auto result = collection.estimated_document_count(opts);
std::cout << "Estimated number of documents: " << result << std::endl;
Estimated number of documents: 9500

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