简介
欢迎访问官方 MongoDB C 驱动程序的文档站点。您可以将驱动程序添加到应用程序中,以便在 C 语言中与 MongoDB 进行交互。您可以按照 获取 MongoDB C 驱动程序库的说明下载所需的库 libmongoc 和 libbson,或者按照我们的教程建立一个可运行的项目。
连接到 MongoDB Atlas
您可以使用以下连接片段来测试与 Atlas 上 MongoDB 部署的连接:
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。此次命名更改后,所有概念和功能均保持不变。
在没有 Stable API 的情况下连接到 MongoDB Atlas
如果您使用的 MongoDB 版本或驱动程序不支持 Stable API,则可使用以下代码片段来测试与 Atlas 上 MongoDB 部署的连接:
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; }
兼容性
要学习;了解与每个版本的C驾驶员兼容的MongoDB Server和C语言版本,请参阅兼容性页面。
如何获得帮助
在MongoDB Stack Overflow页面 或在MongoDB Reddit 社区中寻求帮助。
访问我们的支持渠道。
在Jira上提交错误或功能请求。