Docs 菜单
Docs 主页
/ / /
C++ 驱动程序
/

计算文档

在此页面上

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

在本指南中,您可以学习;了解如何使用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 群集并加载示例数据集,请参阅Atlas入门指南。

使用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
The collation to use for the operation.
Type: bsoncxx::document::view_or_value
hint
The index to use for the operation.
Type: mongocxx::hint
comment
The comment to attach to the operation.
Type: bsoncxx::types::bson_value::view_or_value
limit
The maximum number of documents to count. This value must be a positive integer.
Type: std::int64_t
max_time
The maximum amount of time in milliseconds that the operation can run.
Type: std::chrono::milliseconds
skip
The number of documents to skip before counting documents.
Type: std::int64_t
read_preference
The read preference to use for the operation.
Type: 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
The maximum amount of time in milliseconds that the operation can run.
Type: std::chrono::milliseconds
comment
The comment to attach to the operation.
Type: bsoncxx::types::bson_value::view_or_value
read_preference
The read preference to use for the operation.
Type: 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 文档:

后退

检索不同字段值