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

C++驱动程序入门

MongoDB C++驱动程序是一个C++包,可用于连接到MongoDB并与部署中存储的数据交互。本指南向您展示如何创建一个应用程序,该程序使用C++驱动程序连接到MongoDB Atlas上托管的MongoDB 集群并查询集群中的数据。

提示

MongoDB Atlas 是完全托管的云数据库服务,旨在托管您的 MongoDB 部署。您可以按照本指南中的步骤创建您的免费(无需信用卡)MongoDB Atlas 部署。

Follow this guide to connect a sample C++ application to a MongoDB Atlas deployment. If you prefer to connect to MongoDB using a different driver or programming language, see our list of official drivers.

1

在开始本教程之前,请确保您已在开发环境中安装以下依赖项:

注意

C++17 之前的配置

尽管C++11 是支持的最低语言版本,但本教程将C++驱动程序配置为使用C++17 标准库,如选择C++17 Polyfill部分所建议。如果要安装 C++17 之前版本配置的驾驶员,设立 CMAKE_CXX_STANDARD 为您的 C++ 版本。然后,驱动程序将自动使用 bsoncxx 库 polyfill 实现来实现所需的 C++17 功能。

2

要从 mongo-cxx-driver Github 存储库下载最新版本的 C++ 驱动程序,请在 shell 中的根目录运行以下命令:

curl -OL https://github.com/mongodb/mongo-cxx-driver/releases/download/r3.11.1/mongo-cxx-driver-r3.11.1.tar.gz
tar -xzf mongo-cxx-driver-r3.11.1.tar.gz
cd mongo-cxx-driver-r3.11.1/build
3

选择与操作系统相对应的标签页,然后从mongo-cxx-driver-r3.11.1/build目录运行以下命令:

cmake .. \
-DCMAKE_BUILD_TYPE=Release \
-DCMAKE_CXX_STANDARD=17

此命令指示 CMake 将mongocxx安装到/usr/local目录中。

4

选择与您的操作系统相对应的标签页并运行以下命令以安装驱动程序:

cmake --build .
sudo cmake --build . --target install

完成这些步骤后,您就已在计算机上安装了C++驱动程序。

您可以在 MongoDB Atlas 上创建一个免费集群来存储和管理您的数据。MongoDB Atlas 在云端托管和管理您的 MongoDB 数据库。

1

Complete the MongoDB Get Started guide to set up a new Atlas account and load sample data into a new free tier MongoDB deployment.

2

创建数据库用户后,请将该用户的用户名和密码保存在一个安全位置,以便在后续步骤中使用。

完成这些步骤后,您就可以在 Atlas 上有一个新的免费集群,并建立数据库用户凭证,以及将示例数据加载到数据库中。

您可以通过提供连接 URI(也称为连接字符串)来连接到 MongoDB 部署,它指示驱动程序如何连接到 MongoDB 部署以及连接时的行为方式。

连接字符串包含部署的主机名或 IP 地址和端口、身份验证机制、用户档案(如果适用)以及连接选项。

要连接到未托管在Atlas上的实例或部署,请参阅选择连接目标指南。

1

To retrieve your connection string for the deployment that you created in the previous step, log into your Atlas account and navigate to the Clusters page under the Database section. Click the Connect button for your new deployment.

Atlas 用户界面的集群部分中的连接按钮

如果您尚未配置数据库用户, MongoDB将提示您创建并配置新用户。

单击 Connect to your application 部分下的 Drivers 按钮,从 Driver 选择菜单中选择“C++”,然后从 Version 选择菜单中选择与您安装的版本最匹配的版本。

确保取消选择 View full code sample 选项以仅查看连接字符串。

2

单击连接string右侧的按钮将其复制到剪贴板,如以下屏幕截图所示:

用户界面中连接 旁边的复制按钮stringAtlas
3

将此连接字符串粘贴到首选文本编辑器中的文件中,并将 <db_password> 占位符替换为数据库用户的密码。连接字符串已填充数据库用户的用户名。

将此文件保存到安全位置,以便在下一步中使用。

完成这些步骤后,您就拥有了与string Atlas集群对应的连接 。

1

在根目录中,在shell中运行以下命令,为此项目创建一个名为 cpp-quickstart 的目录:

mkdir cpp-quickstart

运行以下命令,在cpp-quickstart目录中创建quickstart.cpp应用程序文件:

cd cpp-quickstart
touch quickstart.cpp
2

将以下代码复制并粘贴到quickstart.cpp文件中,该文件将查询sample_mflix数据库中的movies集合:

#include <cstdint>
#include <iostream>
#include <vector>
#include <bsoncxx/builder/basic/document.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
using bsoncxx::builder::basic::kvp;
using bsoncxx::builder::basic::make_document;
int main() {
mongocxx::instance instance;
mongocxx::uri uri("<connection string>");
mongocxx::client client(uri);
auto db = client["sample_mflix"];
auto collection = db["movies"];
auto result = collection.find_one(make_document(kvp("title", "The Shawshank Redemption")));
std::cout << bsoncxx::to_json(*result) << std::endl;
}
3

Replace the <connection string> placeholder with the connection string that you copied from the Create a Connection String step of this guide.

4

在shell中,运行以下命令以编译并运行此应用程序:

c++ --std=c++17 quickstart.cpp $(pkg-config --cflags --libs libmongocxx) -o ./app.out
./app.out

提示

MacOS 用户在运行上述命令后可能会看到以下错误:

dyld[54430]: Library not loaded: @rpath/libmongocxx._noabi.dylib

要解决此错误,请使用-Wl,-rpath链接器选项来设立@rpath ,如以下代码所示:

c++ --std=c++17 quickstart.cpp -Wl,-rpath,/usr/local/lib/ $(pkg-config --cflags --libs libmongocxx) -o ./app.out
./app.out

命令行输出包含有关检索到的电影文档的详细信息:

{ "_id" : { "$oid" : "573a1399f29313caabceeb20" },
"plot" : "Two imprisoned men bond over a number of years, finding solace
and eventual redemption through acts of common decency.",
...
"title" : "The Shawshank Redemption",
...

如果遇到错误或看不到输出,请确保在quickstart.cpp文件中指定了正确的连接字符串并加载了样本数据。

完成这些步骤后,您有一个正常运行的应用程序,它使用驱动程序连接到 MongoDB 部署、对示例数据运行查询并打印结果。

恭喜您完成快速入门教程!

注意

如果您在学习本教程时运行问题,请在MongoDB Stack Overflow页面 或MongoDB Reddit 社区 寻求帮助,或者使用本页面右侧或右下角的 Rate this page标签页提交反馈。

在本教程中,您创建了一个C++应用程序,该应用程序连接到MongoDB Atlas上托管的MongoDB 部署,并检索与查询匹配的文档。

通过以下资源学习有关C++驱动程序的更多信息: