Building CXX MongoDB Driver - linux-on-ibm-z/docs GitHub Wiki
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) has
1.1.3
The instructions provided below specify the steps to build CXX MongoDB Driver 3.10.2 on Linux on IBM Z for following distributions:
- RHEL (8.8, 8.10, 9.2, 9.4)
- SLES (12 SP5, 15 SP5, 15 SP6)
- 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.
export SOURCE_ROOT=/<source_root>/
-
RHEL (8.8, 8.10, 9.2, 9.4)
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 libcurl-devel nghttp2-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, 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)
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
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
cd $SOURCE_ROOT
git clone https://github.com/mongodb/mongo-cxx-driver.git
cd mongo-cxx-driver
git checkout r3.10.2
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local ..
sudo cmake --build .
make
sudo make install
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.
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;
}
}
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" }