对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

数据库和集合

在本指南中,您可以学习如何使用C++驱动程序与MongoDB数据库和集合进行交互。

MongoDB 将数据组织成以下级别的层次结构:

  • 数据库: MongoDB 部署中用于存储集合的顶级数据结构。

  • 集合: MongoDB文档组。 它们类似于关系数据库中的表。

  • Documents: Units that store literal data such as string, numbers, dates, and other embedded documents. For more information about document field types and structure, see the Documents guide in the MongoDB Server manual.

您可以通过对 mongocxx::client对象调用 database() 函数并将数据库名称作为参数传递访问权限数据库。

以下示例将访问名为test_database的数据库:

auto db = client.database("test_database");

或者,您可以使用 mongocxx::client 上的 []操作符作为 database() 函数的简写,如以下代码所示:

auto db = client["test_database"];

您可以通过对 mongocxx::database对象调用 collection() 函数并将集合名称作为参数传递访问权限集合。

以下示例访问名为 test_collection 的集合:

auto coll = database.collection("test_collection");

或者,您可以使用 mongocxx::database 上的 []操作符作为 collection() 函数的简写,如以下代码所示:

auto coll = database["test_collection"];

提示

如果提供的集合名称在数据库中尚不存在,则当您首次向其中插入数据时,MongoDB 会隐式创建该集合。

您可以使用 create_collection() 函数在MongoDB 数据库中显式创建集合。

以下示例创建了一个名为example_collection的collection:

auto coll = database.create_collection("example_collection");

You can specify collection options, such as maximum size and document validation rules, by passing them inside a BSON document as the second parameter to the create_collection() function. For a full list of optional parameters, see the create command documentation in the MongoDB Server manual.

您可以通过调用 list_collections() 函数检索数据库中的集合列表。 该函数返回一个游标,其中包含数据库中的所有集合及其关联的元数据。

以下示例调用 list_collections() 函数并对游标进行迭代以打印结果:

auto cursor = database.list_collections();
for(auto&& doc : cursor) {
std::cout << bsoncxx::to_json(doc) << std::endl;
}

要仅查询数据库中集合的名称,请调用 list_collection_names() 函数,如以下示例所示:

auto list = database.list_collection_names();
for(auto&& name : list) {
std::cout << name << std::endl;
}

有关遍历游标的更多信息,请参阅《从游标访问数据》指南。

您可以使用 drop() 函数从数据库中删除集合。

以下示例删除test_collection集合:

auto coll = database["test_collection"];
coll.drop();

警告

删除集合会删除该集合中的所有数据

从数据库中删除集合会永久删除该集合中的所有文档和所有索引。

仅当不再需要集合中的数据时才删除集合。

您可以使用 drop() 函数从集群中删除数据库。

以下示例删除 test_database数据库:

auto db = client["test_database"];
db.drop();

警告

删除数据库会删除数据库中的所有数据

删除数据库会永久删除该数据库中的所有集合、文档和索引。

仅当不再需要数据库中的数据时才删除数据库。

您可以通过设置读取偏好来控制驱动程序路由读取操作的方式。 您还可以通过设置读关注和写关注来控制驱动程序如何等待副本集上读写操作确认的选项。

默认下,数据库从 mongocxx::client对象继承这些设置,集合从数据库继承这些设置。 但是,您可以在数据库或集合上使用以下函数之一来更改这些设置:

  • read_preference()

  • read_concern()

  • write_concern()

要学习;了解有关读取和写入设置的更多信息,请参阅MongoDB Server手册中的以下指南:

此示例演示如何使用以下函数配置数据库的读取设置:

  • read_preference():将读取偏好(read preference)设置为 k_secondary

  • read_concern():将读关注(read concern)设置为 k_majority

auto db = client["test_database"];
mongocxx::read_preference rp;
rp.mode(mongocxx::read_preference::read_mode::k_secondary);
mongocxx::read_concern rc;
rc.acknowledge_level(mongocxx::read_concern::level::k_majority);
db.read_preference(rp);
db.read_concern(rc);

提示

要查看每个读取偏好(read preference)和读关注(read concern)选项的描述,请参阅以下API文档:

此示例展示了如何使用以下函数指定集合的读关注和写关注(write concern):

  • read_concern():将读关注(read concern)设置为 k_local

  • write_concern():将写关注(write concern)设置为 k_acknowledged

auto coll = client["test_database"]["test_collection"];
mongocxx::read_concern rc;
rc.acknowledge_level(mongocxx::read_concern::level::k_local);
mongocxx::write_concern wc;
wc.acknowledge_level(mongocxx::write_concern::level::k_acknowledged);
coll.read_concern(rc);
coll.write_concern(wc);

提示

要查看每个读关注和写关注(write concern)级别的描述,请参阅以下API文档:

In the MongoDB Server, you can apply key-value tags to replica-set members according to any criteria you choose. You can then use those tags to target one or more members for a read operation.

默认下, C++驱动程序在选择要读取的节点时会忽略标签。要指示C++驱动程序优先选择某些标签,请创建一个 mongocxx::read_preference 对象并调用其 tags() 节点函数。将您的首选标签作为数组参数传递给 tags()。

在以下代码示例中,传递给 tags() 函数的标签集指示 C++ 驱动程序优先从纽约数据中心 ("dc": "ny") 读取数据,并回退到旧金山数据中心 ("dc": "sf"):

auto tag_set_ny = make_document(kvp("dc", "ny"));
auto tag_set_sf = make_document(kvp("dc", "sf"));
mongocxx::read_preference rp;
rp.mode(mongocxx::read_preference::read_mode::k_secondary);
rp.tags(make_array(tag_set_ny, tag_set_sf).view());

如果多个副本集成员与您指定的读取偏好和标签集匹配,则 C++ 驱动程序会根据其网络探测时间选择最近的副本集成员进行读取。

默认情况下,驱动程序仅使用那些网络探测时间在距离最近的节点 15 毫秒以内的节点进行查询。若要在延迟较高的成员之间分配读取,请在连接字符串 URI 中包含 localThresholdMS 参数。

以下示例连接到在 localhost:27017 上运行的MongoDB 部署,并指定 35 毫秒的本地阈值:

mongocxx::uri uri("mongodb://localhost:27017/?localThresholdMS=35");
mongocxx::client client(uri);

在前面的示例中, C++驱动程序在最近节点的网络探测(ping)时间的 35 毫秒内在匹配节点之间分配读取。

要学习;了解有关本指南中讨论的任何函数的更多信息,请参阅以下API文档: