Docs Menu

Docs HomeMongoDB Drivers

MongoDB C Driver

On this page

  • Introduction
  • Installation
  • Connect to MongoDB Atlas
  • 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).

To connect to a MongoDB Atlas cluster, use the Atlas connection string for your cluster:

#include <mongoc/mongoc.h>
int
main (int argc, char *argv[])
{
mongoc_database_t *database;
mongoc_client_t *client;
mongoc_init ();
// Replace the uri string with your MongoDB deployment's connection string.
client = mongoc_client_new(
"mongodb+srv://<username>:<password>@<cluster-url>/test?retryWrites=true&w=majority"
);
database = mongoc_client_get_database (client, "test");
mongoc_database_destroy (database);
mongoc_client_destroy (client);
mongoc_cleanup ();
return 0;
}

Note

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

See Advanced Connections for more ways to connect.

You can use the Stable API feature starting with MongoDB Server version 5.0 and C Driver version 1.18. When you use the Stable API 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 the Stable API. All concepts and features remain the same with this naming change.

To use this feature, construct a MongoDB client instance, specifying a version of the Stable API:

// Replace <connection string> with your MongoDB deployment's connection string.
const char *uri_string = "<connection string>";
mongoc_uri_t *uri;
mongoc_client_t *client;
mongoc_server_api_t *api;
bson_error_t error;
mongoc_init ();
uri = mongoc_uri_new_with_error (uri_string, &error);
client = mongoc_client_new_from_uri (uri);
// Set the version of the Stable API on the client.
api = mongoc_server_api_new (MONGOC_SERVER_API_V1);
mongoc_client_set_server_api (client, api, &error);

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.

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).

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.

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.

←  Start Developing with MongoDBMongoDB C++ Driver →
Give Feedback
© 2022 MongoDB, Inc.

About

  • Careers
  • Investor Relations
  • Legal Notices
  • Privacy Notices
  • Security Information
  • Trust Center
© 2022 MongoDB, Inc.