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

Retrieve Data

在此页面上

  • Overview
  • 样本数据
  • 查找文档
  • 查找一个文档
  • 查找多个文档
  • 修改查找行为
  • 更多信息
  • API 文档

在本指南中,您可以学习;了解如何使用C++驾驶员通过读取操作从MongoDB集合中检索数据。 您可以对集合调用 find()find_one()方法,以检索与一设立条件匹配的文档。

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

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

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

C++驾驶员包括两种从集合中检索文档的方法: find_one()find() 。 这些方法采用查询过滤并返回一个或多个匹配的文档。 查询过滤是一个对象,用于指定要在查询中检索的文档。

提示

要学习;了解有关查询筛选器的更多信息,请参阅指定查询。

要查找集合中的单个文档,请调用find_one()方法并传递查询过滤,其中指定要查找的文档条件。

find_one()方法返回std::optional< bsoncxx::document::value >的实例。 如果查询过滤与文档匹配,则optional对象包含bsoncxx::document::value类型的值。 如果查询过滤器未匹配任何文档,则optional对象不包含任何值。

如果查询过滤匹配多个文档,则find_one()方法会从检索到的结果中返回第一个匹配的文档。

提示

当您知道只有一个匹配文档或者您只对第一个匹配项感兴趣时, find_one()方法非常有用。

以下示例使用find_one()方法查找name字段值为"LinkedIn"的第一个文档:

auto result = collection.find_one(make_document(kvp("name", "LinkedIn")));
std::cout << bsoncxx::to_json(*result) << std::endl;
{ "_id" : { "$oid" : "52cdef7c4bab8bd675297e0c" }, "name" : "LinkedIn", "permalink" : "linkedin",
"crunchbase_url" : "http://www.crunchbase.com/company/linkedin", "homepage_url" : "http://linkedin.com",
...

提示

排序顺序

如果未指定排序条件,find_one() 方法将按自然顺序返回磁盘上的第一份文档。

要学习;了解有关排序的更多信息,请参阅《指定要返回的文档》指南中的排序部分。

要查找集合中的多个文档,请将查询筛选器传递给 find() 方法,其中指定要检索的文档条件。

以下示例使用find()方法查找founded_year字段值为1970的所有文档:

auto cursor = collection.find(make_document(kvp("founded_year", 1970)));

find()方法返回一个mongocxx::cursor实例,您可以对其进行迭代以查看匹配的文档。 游标是一种机制,允许应用程序迭代数据库结果,同时在给定时间仅在内存中保存其中的子集。 当find()方法返回大量文档时,游标非常有用。

您可以使用for-in循环遍历游标中的文档,如以下示例所示:

auto cursor = collection.find(make_document(kvp("founded_year", 1970)));
for(auto&& doc : cursor) {
std::cout << bsoncxx::to_json(doc) << std::endl;
}
{ "_id" : { "$oid" : "52cdef7d4bab8bd675298be4" }, "name" : "Mitsubishi Motors",
"permalink" : "mitsubishi-motors", "crunchbase_url" : "http://www.crunchbase.com/company/mitsubishi-motors",
...
{ "_id" : { "$oid" : "52cdef7e4bab8bd67529b996" }, "name" : "Western Digital",
"permalink" : "western-digital", "crunchbase_url" : "http://www.crunchbase.com/company/western-digital",
...
{ "_id" : { "$oid" : "52cdef7e4bab8bd67529b9f1" }, "name" : "Celarayn", "permalink" : "celarayn",
"crunchbase_url" : "http://www.crunchbase.com/company/celarayn",
...

注意

查找所有文档

要查找集合中的所有文档,请将空筛选器传递给find()方法:

auto cursor = collection.find({})

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

字段
说明
batch_size
The number of documents to return per batch.
Type: std::int32_t
collation
The collation to use for the operation.
Type: bsoncxx::document::view_or_value
comment
The comment to attach to the operation.
Type: bsoncxx::string::view_or_value
cursor_type
The type of cursor to use for the operation.
Type: cursor::type
limit
The maximum number of documents the operation can return.
Type: std::int64_t
skip
The number of documents to skip before returning results.
Type: std::int64_t
sort
The order in which the operation returns matching documents.
Type: bsoncxx::document::view_or_value

以下示例使用find()方法查找number_of_employees字段值为1000的所有文档,并指示操作最多返回5结果:

mongocxx::options::find opts;
opts.limit(5);
auto cursor = collection.find(make_document(kvp("number_of_employees", 1000)), opts);

有关mongocxx::options::find 对象字段的完整列表,请参阅 API文档。

如需了解有关查询过滤器的更多信息,请参阅指定查询

有关使用C++驾驶员检索文档的可运行代码示例,请参阅从MongoDB读取数据。

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

  • find()

  • find_one()

  • limit()

后退

从 MongoDB 读取数据