Docs Menu
Docs Home
/ /

Controlador C de MongoDB

Bienvenido al sitio de documentación del controlador oficial de MongoDB C. Puede agregar el controlador a su aplicación para trabajar con MongoDB en C. Descargue las bibliotecas necesarias. libmongoc y libbson, siguiendo las Obtenga instrucciones sobre las bibliotecas del controlador C de MongoDB o configure un proyecto ejecutable siguiendo nuestro tutorial.

  • Tutorial

  • Usage Guide

  • Centro del desarrollador de MongoDB

  • Referencia de API

  • Registro de cambios

  • Código fuente

  • Ejemplos

  • Ejemplos adicionales de BSON

Puede utilizar el siguiente fragmento de conexión para probar su conexión a su implementación de MongoDB en Atlas:

#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;
}

Este fragmento de conexión utiliza la función API estable. Puede acceder a esta función al conectarse a MongoDB Server v5.0 y versiones posteriores y usar el controlador C v1.18 y versiones posteriores.

Cuando se usa esta característica, se puede actualizar el driver o servidor sin preocuparte por problemas de compatibilidad con versiones anteriores con cualquier comando cubierto por Stable API.

Nota

A partir de febrero de 2022, la API versionada se conoce como Stable API. Todos los conceptos y características permanecen iguales con este cambio de nombre.

Si está utilizando una versión de MongoDB o el controlador que no es compatible con la API estable, puede usar el siguiente fragmento de código para probar su conexión a su implementación de MongoDB en Atlas:

#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;
}

Para obtener información sobre las versiones de MongoDB Server y el lenguaje C que son compatibles con cada versión del controlador C, consulte la página Compatibilidad.

Next

Libbson

En esta página