Skip to content

Building CXX MongoDB Driver

linuxonz edited this page Feb 3, 2025 · 47 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, 24.04, 24.10) has 1.1.3

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

  • RHEL (8.8, 8.10, 9.2, 9.4, 9.5)
  • SLES 15 SP6
  • Ubuntu (20.04, 22.04, 24.04, 24.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.

1. Build and Install CXX MongoDB Driver 4.0.0

1.1. Install dependencies

export SOURCE_ROOT=/<source_root>/
  • RHEL (8.8, 8.10, 9.2, 9.4, 9.5)

    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 15 SP6

    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, 24.04, 24.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 r4.0.0
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local ..
sudo cmake --build .
make
sudo make install

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