Docs Menu

Docs HomeDevelop ApplicationsMongoDB Drivers

MongoDB C Driver

On this page

  • Introduction
  • Installation
  • Connect to MongoDB Atlas
  • Connect to MongoDB Atlas Without the Stable API
  • Connect to a MongoDB Server on Your Local Machine
  • 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, from mongoc.org or set up a runnable project by following our tutorial.

See Installing the MongoDB C Driver (libmongoc) and BSON library (libbson).

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 which you can use when connecting to MongoDB Server v5.0 and later and 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;
}

If you need to run a MongoDB server on your local machine for development purposes instead of using an Atlas cluster, you need to complete the following:

  1. Download the Community or Enterprise version of MongoDB Server.

  2. Install and configure MongoDB Server.

  3. Start the server.

Important

Always secure your MongoDB server from malicious attacks. See our Security Checklist for a list of security recommendations.

After you successfully start your MongoDB server, specify your connection string in your driver connection code.

If your MongoDB Server is running locally, you can use the connection string "mongodb://localhost:<port>" where <port> is the port number you configured your server to listen for incoming connections.

If you need to specify a different hostname or IP address, see our Server Manual entry on Connection Strings.

To test whether you can connect to your server, replace the connection string in the Connect to MongoDB Atlas code example and run it.

See Advanced Connections for more ways to connect.

Note

For information about connecting to Atlas Serverless, see the Serverless Instance Limitations page for the minimum driver version you need.

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.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.26
1.25
1.24
<= 1.23

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

←  Start Developing with MongoDBMongoDB .NET/C# Driver →