Skip to content

Building C MongoDB Driver

linuxonz edited this page Mar 28, 2024 · 63 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.6, 8.8, 8.9, 9.0, 9.2, 9.3) have 1.24.3
  • Ubuntu 20.04 has 1.16.1
  • Ubuntu 22.04 has 1.21.0
  • Ubuntu 23.10 has 1.24.3

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

  • RHEL (7.8, 7.9, 8.6, 8.8, 8.9, 9.0, 9.2, 9.3)
  • Ubuntu (20.04, 22.04, 23.10)

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

Step 1: Install MongoDB C Driver

1.1) Install dependencies

  • RHEL (7.8, 7.9)

    sudo yum install -y python3-devel devtoolset-7 devtoolset-7-elfutils-libelf-devel git pkgconfig kernel-devel kmod perl make cmake gcc-c++ wget tar diffutils openssl-devel texlive
    # switch to GCC 7   
    source /opt/rh/devtoolset-7/enable
    # Create softlink for python3
    sudo ln -sf /usr/bin/python3 /usr/bin/python
    • CMake 3.x
      cd $SOURCE_ROOT
      wget https://cmake.org/files/v3.26/cmake-3.26.4.tar.gz
      tar xzf cmake-3.26.4.tar.gz
      cd cmake-3.26.4
      ./configure --prefix=/usr/local
      make && sudo make install
      export PATH=$PATH:/usr/local/bin
  • RHEL (8.6, 8.8, 8.9, 9.0, 9.2, 9.3)

    sudo yum install -y python3-devel gcc gcc-c++ git pkgconfig kernel-devel kmod perl make cmake gcc-c++ wget tar diffutils openssl-devel texlive
    sudo ln -sf /usr/bin/python3 /usr/bin/python  
  • Ubuntu (20.04, 22.04, 23.10)

    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.26.1.tar.gz
tar -xzf 1.26.1.tar.gz
cd mongo-c-driver-1.26.1
mkdir cmake-build
cd cmake-build
cmake -D ENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF -D BUILD_VERSION="1.26.1" ..
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.26.1");

  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" : "64abb51503249283f00d14a1" }, "company" : "IBM", "project" : "MongoDB Driver", "language" : "C", "version" : "1.26.1" }
{ "_id" : { "$oid" : "64abb51503249283f00d14a2" }, "line" : "0" }
{ "_id" : { "$oid" : "64abb51503249283f00d14a3" }, "line" : "1" }
{ "_id" : { "$oid" : "64abb51503249283f00d14a4" }, "line" : "2" }

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

References:

Clone this wiki locally