Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs Menu
Docs Home
/ /

MongoDB C 드라이버

공식 MongoDB C 드라이버의 설명 사이트에 오신 것을 환영합니다. C에서 MongoDB와 함께 작동하도록 애플리케이션에 드라이버를 추가할 수 있습니다. MongoDB C 드라이버 라이브러리 가져오기 지침에 따라 필수 라이브러리 libmongoclibbson를 다운로드하거나, 튜토리얼에 따라 실행 가능한 프로젝트를 설정하세요.

  • 튜토리얼

  • Usage Guide

  • MongoDB 개발자 센터

  • API 참조

  • ChangeLog

  • 소스 코드

  • 예시

  • 추가 BSON 예시

다음 연결 스니펫을 사용하여 Atlas에서 MongoDB deployment에 대한 연결을 테스트할 수 있습니다.

#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 v5.0 이상에 연결하고 C 드라이버 v1.18 이상을 사용할 때 이 기능에 액세스할 수 있습니다.

이 기능을 사용하면 스테이블 API에서 다루는 모든 명령의 하위 호환성 문제에 대한 걱정 없이 드라이버나 서버를 업데이트할 수 있습니다.

참고

2022년 2월부터 버전이 지정된 APIStable API로 표시됩니다. 이름은 변경되나 모든 개념과 기능은 동일하게 유지됩니다.

Stable API에 대한 지원이 없는 MongoDB 버전 또는 드라이버를 사용하는 경우, 다음 코드 조각을 사용하여 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 드라이버의 버전이 명시되어 있습니다.

첫 번째 열에는 드라이버 버전이 나열됩니다.

중요

MongoDB는 서버 버전의 수명 종료(EOL) 날짜 이후 3년 동안 MongoDB Server와 드라이버 간의 호환성을 보장합니다. MongoDB 릴리스 및 EOL 날짜에 대한 자세한 내용은 MongoDB 소프트웨어 수명 주기 일정을 참조하세요.

Icon
설명

모든 기능이 지원됩니다.

드라이버 버전은 MongoDB 버전에서 작동하지만 모든 새로운 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 드라이버 버전
C17/C18
C11
C99
C89

1.27

1.26

1.25

1.24

<= 1.23

호환성 테이블을 읽는 방법은 MongoDB 호환성 테이블에 대한 가이드에서 자세히 확인하세요.

돌아가기

C

이 페이지의 내용