Introdução
Bem-vindo ao site de documentação do driver C oficial do MongoDB. Você pode adicionar o driver ao seu aplicativo para trabalhar com o MongoDB em C. Baixe as bibliotecas necessárias, libmongoc e libbson, seguindo as instruções Obtendo as bibliotecas do driver C do MongoDB ou configure um projeto executável seguindo nosso tutorial.
Ligar ao MongoDB Atlas
Você pode utilizar o seguinte trecho de conexão para testar sua conexão com seu MongoDB deployment no Atlas:
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; }
Este trecho de conexão usa a funcionalidade Stable API . Você pode acessar esse recurso ao se conectar ao MongoDB Server v5.0 e posterior e usar o driver C v1.18 e posterior.
Ao usar esse recurso, você pode atualizar seu driver ou servidor sem se preocupar com problemas de compatibilidade com versões anteriores de qualquer comando coberto pela API estável.
Observação
A partir de fevereiro de 2022, a API versionada é conhecida como API estável. Todos os conceitos e recursos permanecem os mesmos com esta mudança de nomenclatura.
Conecte-se ao Atlas MongoDB sem a API estável
Se você estiver usando uma versão do MongoDB ou o driver que não tem suporte para a API estável, você pode usar o seguinte trecho de código para testar sua conexão com a MongoDB deployment no Atlas:
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; }
Compatibilidade
Para saber mais sobre as versões do MongoDB Server e a linguagem C compatíveis com cada versão do driver C, consulte a página Compatibilidade.
Como obter ajuda
Pede ajuda na página do MongoDB Stack Overflow ou na comunidade do MongoDB Reddit.
Visite nossos Canais de Suporte.
Registre um bug ou solicitação de recurso no JIRA.