MongoDB C Driver

A Cross Platform MongoDB Client Library for C

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.


Example: Count documents in a collection

#include <bson.h>
#include <mongoc.h>
#include <stdio.h>

main ()
   mongoc_client_t *client;
   mongoc_collection_t *collection;
   bson_error_t error;
   int64_t count;

   mongoc_init ();

   client = mongoc_client_new ("mongodb://localhost:27017/");
   collection = mongoc_client_get_collection (client, "mydb", "mycoll");
   count = mongoc_collection_count (collection, MONGOC_QUERY_NONE, NULL, 0, 0, NULL, &error);

   if (count < 0) {
      fprintf (stderr, "%s\n", error.message);
   } else {
      printf ("%" PRId64 "\n", count);

   mongoc_collection_destroy (collection);
   mongoc_client_destroy (client);
   mongoc_cleanup ();

   return count < 0 ? -1 : 0;

How To Ask For Help

Documentation for Older Versions