MongoDB C Driver
On this page
Introduction
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.
Installation
See Installing the MongoDB C Driver (libmongoc) and BSON library (libbson).
Connect to MongoDB Atlas
You can use the following connection snippet to test your connection to your MongoDB deployment on Atlas:
int main (int argc, char **argv) { 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, reply; // Initialize the MongoDB C Driver. mongoc_init (); // Replace the <connection string> with your MongoDB deployment's connection string. client = mongoc_client_new("<connection string>"); // Set the version of the Stable API on the client. api = mongoc_server_api_new (MONGOC_SERVER_API_V1); if (!mongoc_client_set_server_api (client, api, &error)) { // Error condition. printf("Error: %s\n", error.message); return 0; } // Get a handle on the "admin" database. database = mongoc_client_get_database (client, "admin"); // Ping the database. command = BCON_NEW("ping", BCON_INT32(1)); if (mongoc_database_command_simple(database, command, NULL, &reply, &error)) { printf("Pinged your deployment. You successfully connected to MongoDB!\n"); } else { // Error condition. printf("Error: %s\n", error.message); return 0; } // Perform Cleanup. bson_destroy (&reply); bson_destroy (command); mongoc_database_destroy (database); mongoc_client_destroy (client); mongoc_cleanup (); return 0; }
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 Feburary 2022, the Versioned API is known as the Stable API. All concepts and features remain the same with this naming change.
Connect to MongoDB Atlas Without the Stable API
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:
int main (int argc, char **argv) { mongoc_client_t *client = NULL; bson_error_t error = {0}; mongoc_database_t *database = NULL; bson_t *command = NULL, reply; // Initialize the MongoDB C Driver. mongoc_init (); // Replace the <connection string> with your MongoDB deployment's connection string. client = mongoc_client_new("<connection string>"); // Get a handle on the "admin" database. database = mongoc_client_get_database (client, "admin"); // Ping the database. command = BCON_NEW("ping", BCON_INT32(1)); if (mongoc_database_command_simple(database, command, NULL, &reply, &error)) { printf("Pinged your deployment. You successfully connected to MongoDB!\n"); } else { // Error condition. printf("Error: %s\n", error.message); return 0; } // Perform Cleanup. bson_destroy (&reply); bson_destroy (command); mongoc_database_destroy (database); mongoc_client_destroy (client); mongoc_cleanup (); return 0; }
Connect to a MongoDB Server on Your Local Machine
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:
Download the Community or Enterprise version of MongoDB Server.
Install and configure MongoDB Server.
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.
Compatibility
MongoDB Compatibility
The following compatibility table specifies the recommended version(s) of the MongoDB C driver for use with a specific version of MongoDB.
The first column lists the driver version(s).
Compatibility Table Legend
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 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.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.
Language Compatibility
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 Versions | C11 | C99 | C89 |
---|---|---|---|
All Versions | ✓ | ✓ | ✓ |
For more information on how to read the compatibility tables, see our guide on MongoDB Compatibility Tables.
How to get help
Ask questions on our MongoDB Community Forums.
Visit our Support Channels.
File a bug or feature request on JIRA.