“文档” 菜单
文档首页
/ /

MongoDB C 驱动程序

在此页面上

  • 简介
  • 连接到 MongoDB Atlas
  • 在没有 Stable API 的情况下连接到 MongoDB Atlas
  • 兼容性

欢迎访问官方 MongoDB C 驱动程序的文档站点。您可以将驱动程序添加到您的应用程序中,以便在 C 语言中使用 MongoDB。按照获取 MongoDB C 驱动程序库说明下载所需的库、 libmongoclibbson ,或设置一个可运行的项目按照我们的教程进行操作。

  • 教程

  • 使用指南

  • MongoDB 开发者中心

  • API 参考

  • 变更日志

  • 源代码

  • 举例

  • 其他 BSON 示例

您可以使用以下连接片段来测试与 Atlas 上 MongoDB 部署的连接:

#include <mongoc/mongoc.h>
int main(void) {
mongoc_client_t *client = NULL;
bson_error_t error = {0};
mongoc_server_api_t *api = NULL;
mongoc_database_t *database = NULL;
bson_t *command = NULL;
bson_t reply = BSON_INITIALIZER;
int rc = 0;
bool ok = true;
// Initialize the MongoDB C Driver.
mongoc_init();
client = mongoc_client_new("<connection string>");
if (!client) {
fprintf(stderr, "Failed to create a MongoDB client.\n");
rc = 1;
goto cleanup;
}
// Set the version of the Stable API on the client.
api = mongoc_server_api_new(MONGOC_SERVER_API_V1);
if (!api) {
fprintf(stderr, "Failed to create a MongoDB server API.\n");
rc = 1;
goto cleanup;
}
ok = mongoc_client_set_server_api(client, api, &error);
if (!ok) {
fprintf(stderr, "error: %s\n", error.message);
rc = 1;
goto cleanup;
}
// Get a handle on the "admin" database.
database = mongoc_client_get_database(client, "admin");
if (!database) {
fprintf(stderr, "Failed to get a MongoDB database handle.\n");
rc = 1;
goto cleanup;
}
// Ping the database.
command = BCON_NEW("ping", BCON_INT32(1));
ok = mongoc_database_command_simple(
database, command, NULL, &reply, &error
);
if (!ok) {
fprintf(stderr, "error: %s\n", error.message);
rc = 1;
goto cleanup;
}
bson_destroy(&reply);
printf("Pinged your deployment. You successfully connected to MongoDB!\n");
// Perform cleanup.
cleanup:
bson_destroy(command);
mongoc_database_destroy(database);
mongoc_server_api_destroy(api);
mongoc_client_destroy(client);
mongoc_cleanup();
return rc;
}

此连接片段使用stable API功能。 您可以在连接到 MongoDB Server v 5时使用此功能。 0及更高版本,并使用 C 驱动程序 v 1 。 18及更高版本。

使用此功能时,可以更新驱动程序或服务器,而不必担心 Stable API 涵盖的任何命令的向后兼容性问题。

注意

从 2022 年 2 月开始,版本化 API被称为 Stable API。此次命名更改后,所有概念和功能均保持不变。

如果您使用的 MongoDB 版本或驱动程序不支持 Stable API,则可使用以下代码片段来测试与 Atlas 上 MongoDB 部署的连接:

#include <mongoc/mongoc.h>
int main(void) {
mongoc_client_t *client = NULL;
bson_error_t error = {0};
mongoc_database_t *database = NULL;
bson_t *command = NULL;
bson_t reply = BSON_INITIALIZER;
int rc = 0;
bool ok = true;
// Initialize the MongoDB C Driver.
mongoc_init();
client = mongoc_client_new("<connection string>");
if (!client) {
fprintf(stderr, "Failed to create a MongoDB client.\n");
rc = 1;
goto cleanup;
}
// Get a handle on the "admin" database.
database = mongoc_client_get_database(client, "admin");
if (!database) {
fprintf(stderr, "Failed to get a MongoDB database handle.\n");
rc = 1;
goto cleanup;
}
// Ping the database.
command = BCON_NEW("ping", BCON_INT32(1));
ok = mongoc_database_command_simple(
database, command, NULL, &reply, &error
);
if (!ok) {
fprintf(stderr, "error: %s\n", error.message);
rc = 1;
goto cleanup;
}
bson_destroy(&reply);
printf("Pinged your deployment. You successfully connected to MongoDB!\n");
// Perform cleanup.
cleanup:
bson_destroy(command);
mongoc_database_destroy(database);
mongoc_client_destroy(client);
mongoc_cleanup();
return rc;
}

本部分的兼容性表指定了与特定版本的 MongoDB 一起使用的 MongoDB C 驱动程序的推荐版本。

第一列列出驱动程序版本。

重要

在服务器版本生命周期结束 (EOL) 日期之后的三年内,MongoDB 将确保 MongoDB Server 与驱动程序兼容。要了解有关 MongoDB 版本和 EOL 日期的更多信息,请参阅 MongoDB 软件生命周期时间表

圖標
解释
支持所有功能。
该驱动程序版本将与 MongoDB 版本一起使用,但并不支持所有新的 MongoDB 功能。
无标记
驱动程序版本未使用 MongoDB 版本测试。
C 驱动程序版本
MongoDB 7.0
MongoDB 6.0
MongoDB 5.0
MongoDB 4.4
MongoDB 4.2
MongoDB 4.0
MongoDB 3.6
MongoDB 3.4
MongoDB 3.2
MongoDB 3.0
MongoDB 2.6
1 。 27
1 。 26
1 。 25
1 。 24
1 。 23
1 。 22
1 。 21
1 。 20
1 。 19
1 。 18
[1]
1 。 17
1 。 16
1 。 15
1 。 14
1 。 13
1 。 12
1 。 11
1 。 10
1 。 9
1 。 8
1 。 7
1 。 6
1 。 5
1 。 4
1 。 3
1 。 2
1 。 1
1 。 0
[1] 1.18 驱动程序不支持从节点上的快照读取。有关详细信息,请参阅 MongoDB Server 版本 5.0 发布说明。

此驱动程序不支持早期版本的 MongoDB。

以下兼容性表指定了与特定版本 C 搭配使用的 MongoDB C 驱动程序的推荐版本。

第一列列出驱动程序版本。

C 驱动程序版本
C 17 /C 18
C11
C99
C89
1 。 27
1 。 26
1 。 25
1 。 24
<= 1.23

有关如何阅读兼容性表的更多信息,请参阅我们的 MongoDB 兼容性表指南。

来年

libbson