Docs Menu
Docs Home
/ /

MongoDB C Driver

On this page

  • Introduction
  • Connect to MongoDB Atlas
  • Connect to MongoDB Atlas Without the Stable API
  • Compatibility

Welcome to the documentation site for the official MongoDB C driver. You can add the driver to your application to work with MongoDB in C. Download the required libraries, libmongoc and libbson, by following the Obtaining the MongoDB C Driver Libraries instructions or set up a runnable project by following our tutorial.

  • Tutorial

  • Usage Guide

  • MongoDB Developer Center

  • API Reference

  • Changelog

  • Source Code

  • Examples

  • Additional BSON Examples

You can use the following connection snippet to test your connection to your MongoDB deployment on 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;
}

This connection snippet uses the Stable API feature. You can access this feature when connecting to MongoDB Server v5.0 and later and using the C driver v1.18 and later.

When you use this feature, you can update your driver or server without worrying about backward compatibility issues with any commands covered by the Stable API.

Note

Starting from February 2022, the Versioned API is known as the Stable API. All concepts and features remain the same with this naming change.

If you are using a version of MongoDB or the driver that lacks support for the Stable API, you can use the following code snippet to test your connection to your MongoDB deployment on 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;
}

The compatibility table in this section specifies the recommended version or versions of the MongoDB C driver for use with a specific version of MongoDB.

The first column lists the driver version.

Important

MongoDB ensures compatibility between the MongoDB Server and the drivers for three years after the server version's end of life (EOL) date. To learn more about the MongoDB release and EOL dates, see MongoDB Software Lifecycle Schedules.

Icon
Explanation
All features are supported.
The Driver version will work with the MongoDB version, but not all new MongoDB features are supported.
No mark
The Driver version is not tested with the MongoDB version.
C Driver Version
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] The 1.18 driver does not support snapshot reads on secondaries. For more information, see the MongoDB Server version 5.0 release notes.

The driver does not support older versions of MongoDB.

The following compatibility table specifies the recommended version(s) of the MongoDB C driver for use with a specific version of C.

The first column lists the driver version(s).

C Driver Version
C17/C18
C11
C99
C89
1.27
1.26
1.25
1.24
<= 1.23

For more information on how to read the compatibility tables, see our guide on MongoDB Compatibility Tables.

libbson →