Navigation

MongoDB C Driver

On this page

  • Introduction
  • Installation
  • Connect to MongoDB Atlas
  • Versioned API
  • Connect to a MongoDB Server on Your Local Machine
  • Compatibility

The MongoDB C Driver, also known as “libmongoc”, is the official client library for C applications, and provides a base for MongoDB drivers in higher-level languages.

The library is compatible with all major platforms. It depends on libbson to create and parse BSON data.

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 Versioned API feature starting with MongoDB Server version 5.0 and C Driver version 1.18. When you use the Versioned API feature, you can update your driver or server without worrying about backward compatibility issues with any commands covered by the Versioned API. To use this feature, construct a MongoDB client instance, specifying a version of the Versioned 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 Versioned 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.

C Driver Version
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.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.

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.

Give Feedback

On this page

  • Introduction
  • Installation
  • Connect to MongoDB Atlas
  • Versioned API
  • Connect to a MongoDB Server on Your Local Machine
  • Compatibility