Skip to content

Building CXX MongoDB Driver

linuxonz edited this page Mar 22, 2024 · 42 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 (20.04, 22.04, 23.10) has 1.1.3

The instructions provided below specify the steps to build CXX MongoDB Driver 3.10.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)
  • SLES (12 SP5, 15 SP5)
  • 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.

Step 1: Build and Install CXX MongoDB Driver

1.1) Install dependencies

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

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

      cd $SOURCE_ROOT
      wget https://cmake.org/files/v3.26/cmake-3.26.0.tar.gz
      tar -xzvf cmake-3.26.0.tar.gz
      cd cmake-3.26.0
      ./bootstrap --prefix=/usr
      make
      sudo make install
      
    • GCC 7.5.0

      cd $SOURCE_ROOT
      mkdir gcc
      cd gcc    
      wget https://ftpmirror.gnu.org/gcc/gcc-7.5.0/gcc-7.5.0.tar.xz
      tar -xf gcc-7.5.0.tar.xz
      cd gcc-7.5.0
      ./contrib/download_prerequisites
      mkdir objdir
      cd objdir
      ../configure --disable-multilib --enable-languages=c,c++
      make
      sudo make install
      
      export PATH=/usr/local/bin:$PATH
      export CC=/usr/local/bin/gcc
      export CXX=/usr/local/bin/g++
      export LD_LIBRARY_PATH=/usr/local/lib:/usr/local/lib64:$LD_LIBRARY_PATH
      
  • RHEL (8.6, 8.8, 8.9, 9.0, 9.2, 9.3)

    sudo yum install -y cyrus-sasl-devel gcc-c++ make openssl-devel pkgconfig snappy-devel git tar wget curl diffutils cmake python3 libarchive libzstd-devel
  • SLES 12 SP5

    sudo zypper install -y cmake cyrus-sasl-devel gcc7 gcc7-c++ make gzip libopenssl-devel pkg-config snappy-devel git tar wget curl libzstd-devel
    sudo ln -sf /usr/bin/gcc-7 /usr/bin/gcc
    sudo ln -sf /usr/bin/g++-7 /usr/bin/g++
    sudo ln -sf /usr/bin/gcc /usr/bin/cc
    sudo ln -sf /usr/bin/g++ /usr/bin/c++
    • CMake 3.26.0

      cd $SOURCE_ROOT
      wget https://cmake.org/files/v3.26/cmake-3.26.0.tar.gz
      tar -xzvf cmake-3.26.0.tar.gz
      cd cmake-3.26.0
      ./bootstrap --prefix=/usr
      make
      sudo make install
      
  • SLES (15 SP5)

    sudo zypper install -y cmake cyrus-sasl-devel gcc-c++ make libopenssl-devel pkg-config snappy-devel git tar wget curl gzip libzstd-devel
  • Ubuntu (20.04, 22.04, 23.10)

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

1.2) Build and Install MongoDB C Driver

cd $SOURCE_ROOT
git clone https://github.com/mongodb/mongo-c-driver.git
cd mongo-c-driver
git checkout 1.25.0
mkdir cmake-build
cd cmake-build
cmake -DENABLE_AUTOMATIC_INIT_AND_CLEANUP=OFF -DMONGOC_TEST_USE_CRYPT_SHARED=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.10.1
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local ..
sudo cmake --build .
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. To run this validation test, MongoDB must be running on the default port, 27017.

2.1) 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.2) 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" : "63490e2590d3dbb48c0410c1" }, "hello" : "world" }

References

Clone this wiki locally