Navigation

MongoDB C Driver

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 ();
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;
}

See Advanced Connections for more ways to connect.

To connect to a database running locally on its default port, change the connection string to "mongodb://localhost".

To connect to a database running locally, change the connection string to "mongodb://localhost:<port>".

Your mongod instance must be running to successfully connect to your database. For information on how to start your mongod instance, see the Manage mongod Processes Server Manual Entry.

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

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
  • Connect to localhost
  • Compatibility