Skip to content

Building CXX MongoDB Driver

aborkar-ibm edited this page Dec 15, 2021 · 49 revisions

Building CXX MongoDB Driver

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

  • Ubuntu 18.04 has 1.1.2
  • Ubuntu (20.04, 21.04, 21.10) has 1.1.3

The instructions provided below specify the steps to build CXX MongoDB Driver 3.6.6 on Linux on IBM Z for following distributions:

  • RHEL (7.8, 7.9, 8.2, 8.4, 8.5)
  • SLES (12 SP5, 15 SP2, 15 SP3)
  • Ubuntu (18.04, 20.04, 21.04, 21.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.

Step 1: Build and Install CXX MongoDB Driver 3.6.6

1.1) Install dependencies

export SOURCE_ROOT=/<source_root>/
  • RHEL (7.8, 7.9)

    sudo yum install -y cyrus-sasl-devel gcc-c++ git make openssl-devel pkgconfig snappy tar wget
    • CMake 3.3.2

      cd $SOURCE_ROOT
      wget https://cmake.org/files/v3.3/cmake-3.3.2.tar.gz
      tar -xzvf cmake-3.3.2.tar.gz
      cd cmake-3.3.2
      ./bootstrap --prefix=/usr
      make
      sudo make install
      
  • RHEL (8.2, 8.4, 8.5)

    sudo yum install -y cyrus-sasl-devel gcc-c++ git make openssl-devel pkgconfig snappy tar wget diffutils cmake python3 libarchive
  • SLES (12 SP5, 15 SP2, 15 SP3)

    sudo zypper install -y cmake cyrus-sasl-devel gcc-c++ git make libopenssl-devel pkg-config snappy-devel tar wget gzip
  • Ubuntu (18.04, 20.04, 21.04, 21.10)

    sudo apt-get update
    sudo apt-get install -y cmake gcc g++ git libsasl2-dev libssl-dev libsnappy-dev make pkg-config tar wget python3

1.2) Build and Install MongoDB C Driver

cd $SOURCE_ROOT
wget https://github.com/mongodb/mongo-c-driver/releases/download/1.19.2/mongo-c-driver-1.19.2.tar.gz
tar -xzvf mongo-c-driver-1.19.2.tar.gz
cd mongo-c-driver-1.19.2
mkdir cmake-build
cd cmake-build
cmake -DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF ..
make
sudo make install

1.3) Build and Install MongoDB CXX Driver

cd $SOURCE_ROOT
git clone https://github.com/mongodb/mongo-cxx-driver.git
cd mongo-cxx-driver
git checkout r3.6.6
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local ..
sudo make EP_mnmlstc_core
#Apply this patch to fix build error with Glibc 2.34(Only for Ubuntu 21.10)
sed -i "s/static constexpr std::size_t sigStackSize = 32768 >= MINSIGSTKSZ ? 32768 : MINSIGSTKSZ;/static constexpr std::size_t sigStackSize = 32768;/g" $SOURCE_ROOT/mongo-cxx-driver/src/third_party/catch/include/catch.hpp #Only to be applied for Ubuntu:21.10
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 CXX MongoDB 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.

mongod > /tmp/mongodb.log &
mongo --host localhost --port 27017

2.2) The Test Code

Save the following source file with the filename test.cpp in any directory:
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 <iostream>

#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>

#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>

int main(int, char**) {
    mongocxx::instance inst{};
    mongocxx::client conn{mongocxx::uri("mongodb://localhost:27017")};

    bsoncxx::builder::stream::document document{};

    auto collection = conn["testdb"]["testcollection"];
    document << "hello" << "world";
    collection.drop();
    collection.insert_one(document.view());
    auto cursor = collection.find({});

    for (auto&& doc : cursor) {
        std::cout << bsoncxx::to_json(doc) << std::endl;
    }
}

2.3) Compile and execute the test

Set environment variables:

# For RHEL and SLES
export LD_LIBRARY_PATH=/usr/local/lib64
export PKG_CONFIG_PATH=/usr/local/lib64/pkgconfig
# For Ubuntu
export LD_LIBRARY_PATH=/usr/local/lib
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

Compile and run the test program:

c++ --std=c++11 test.cpp -o test $(pkg-config --cflags --libs libmongocxx)
./test

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

{ "_id" : { "$oid" : "58f885a951e6e39b297d7ab2" }, "hello" : "world" }

References

Installing the mongocxx driver

Clone this wiki locally