서론
공식 MongoDB C 드라이버의 설명 사이트에 오신 것을 환영합니다. C에서 MongoDB와 함께 작동하도록 애플리케이션에 드라이버를 추가할 수 있습니다. MongoDB C 드라이버 라이브러리 가져오기 지침에 따라 필수 라이브러리 libmongoc 및 libbson를 다운로드하거나, 튜토리얼에 따라 실행 가능한 프로젝트를 설정하세요.
MongoDB Atlas에 연결
다음 연결 스니펫을 사용하여 Atlas에서 MongoDB deployment에 대한 연결을 테스트할 수 있습니다.
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 v5.0 이상에 연결하고 C 드라이버 v1.18 이상을 사용할 때 이 기능에 액세스할 수 있습니다.
이 기능을 사용하면 스테이블 API에서 다루는 모든 명령의 하위 호환성 문제에 대한 걱정 없이 드라이버나 서버를 업데이트할 수 있습니다.
참고
2022년 2월부터 버전이 지정된 API는 Stable API로 표시됩니다. 이름은 변경되나 모든 개념과 기능은 동일하게 유지됩니다.
Stable API 없이 MongoDB Atlas에 연결하기
Stable API에 대한 지원이 없는 MongoDB 버전 또는 드라이버를 사용하는 경우, 다음 코드 조각을 사용하여 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 Community에서 도움을 요청하세요.
지원 채널을 방문하세요.
JIRA.에 버그 또는 기능 요청 제출하세요.