Skip to content

Building C MongoDB Driver

linuxonz edited this page Oct 17, 2024 · 72 revisions

Building MongoDB C Driver

Below versions of MongoDB C Driver are available in respective distributions at the time creation of these build instructions:

  • RHEL (8.8, 8.10, 9.2, 9.4) have 1.28.0
  • Ubuntu 20.04 has 1.16.1
  • Ubuntu 22.04 has 1.21.0
  • Ubuntu 24.04 has 1.26.0

Note: The instructions provided below specify the steps to build C MongoDB Driver version 1.28.0 on Linux on IBM Z for following distributions:

  • Ubuntu (20.04, 22.04, 24.04)

General Notes:

  • When following the steps below please use a standard permission user unless otherwise specified.

  • A directory /<source_root>/ will be referred to in these instructions, this is a temporary writable directory anywhere you'd like to place it.

MongoDB C Driver can be installed with Package Manager as mentioned here

1. Install MongoDB C Driver

1.1. Install dependencies

export SOURCE_ROOT=/<source_root>/
  • Ubuntu (20.04, 22.04, 24.04)
    sudo apt-get update
    sudo apt-get install -y cmake gcc libsasl2-dev libssl-dev make pkg-config tar wget curl
    

1.2. Configure, build and install the MongoDB C Driver

cd $SOURCE_ROOT
wget https://github.com/mongodb/mongo-c-driver/archive/refs/tags/1.28.0.tar.gz
tar -xzf 1.28.0.tar.gz
cd mongo-c-driver-1.28.0
mkdir cmake-build
cd cmake-build
cmake -D ENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF -D BUILD_VERSION="1.28.0" ..
make
sudo make install

Step 2: Basic validation test

The example code given below is used to perform a basic test to ensure that the MongoDB C Driver is working as expected, and can connect to, modify and query a MongoDB server. Instructions to install MongoDB can be found on their official website here.

2.1. Start MongoDB

To run this validation test, MongoDB must be running on the default port, 27017. The following commands are an example of how to start a MongoDB server and then connect to it with the client shell.

Issue the following command to start mongod and to check if it is up, connect to it with the MongoDB shell.

mongosh --host <hostname or IP address> --port 27017

The output should be similar to:

Current Mongosh Log ID:	61e05ef9edddf3d8aac307aa
Connecting to:		mongodb://<hostname or IP address>:27017/?directConnection=true&serverSelectionTimeoutMS=2000
Using MongoDB:		5.0.2
Using Mongosh:		1.0.6
...
test>

2.2. The Test Code

Create a file named test.c with the content shown below. This code connects to a MongoDB server, inserts some documents and then queries the database to read them back and display them. If you are connecting to a remote server then you need to substitute the localhost with the hostname or IP address of the MongoDB server.

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

const char* server="localhost";
const char* test_db="ibm_test_db";
const char* collection_name="mongodb_c_driver";

int main (int argc, char *argv[])
{
  mongoc_client_t *client;
  mongoc_collection_t *collection;
  mongoc_cursor_t *cursor;
  bson_error_t error;
  bson_oid_t oid;
  bson_t *doc, query;
  const bson_t *gotdoc;
  char *str;
  int i;
  char istr[2], server_addr[50];

  sprintf(server_addr, "mongodb://%s:27017/", server);

  mongoc_init ();

  client = mongoc_client_new (server_addr);

  collection = mongoc_client_get_collection (client, test_db, collection_name);

  if (!mongoc_collection_drop (collection, &error)) {
      printf ("%s\n", error.message);
  }

  doc = bson_new ();
  BSON_APPEND_UTF8 (doc, "company", "IBM");
  BSON_APPEND_UTF8 (doc, "project", "MongoDB Driver");
  BSON_APPEND_UTF8 (doc, "language", "C");
  BSON_APPEND_UTF8 (doc, "version", "1.28.0");

  if (!mongoc_collection_insert (collection, MONGOC_INSERT_NONE, doc, NULL, &error)) {
      printf ("%s\n", error.message);
  }
  bson_destroy (doc);

  for ( i = 0; i < 3; i++) {
      doc = bson_new ();
      sprintf (istr, "%d", i);
      BSON_APPEND_UTF8 (doc, "line", istr);

      if (!mongoc_collection_insert (collection, MONGOC_INSERT_NONE, doc, NULL, &error)) {
          printf ("%s\n", error.message);
      }
      bson_destroy (doc);
  }

  bson_init (&query);

  cursor = mongoc_collection_find_with_opts (collection, &query, NULL, NULL);

  while (mongoc_cursor_next (cursor, &gotdoc)) {
          str = bson_as_json (gotdoc, NULL);
      fprintf (stdout, "%s\n", str);
      bson_free (str);
  }

  bson_destroy (&query);

  mongoc_cursor_destroy (cursor);
  mongoc_collection_destroy (collection);
  mongoc_client_destroy (client);

  return 0;
}

2.3. Execute

Compile and run the test program by:

export LD_LIBRARY_PATH=/usr/local/lib64
export PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig
gcc -o test test.c $(pkg-config --libs --cflags libmongoc-1.0)
./test

The output should be similar to this (Object ID will vary):

{ "_id" : { "$oid" : "66c31a761e75f18972055561" }, "company" : "IBM", "project" : "MongoDB Driver", "language" : "C", "version" : "1.28.0" }
{ "_id" : { "$oid" : "66c31a761e75f18972055562" }, "line" : "0" }
{ "_id" : { "$oid" : "66c31a761e75f18972055563" }, "line" : "1" }
{ "_id" : { "$oid" : "66c31a761e75f18972055564" }, "line" : "2" }

Note: More examples can be found here on the official mongoc tutorial page.

References:

Clone this wiki locally