-
Notifications
You must be signed in to change notification settings - Fork 56
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.
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
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 r4.0.0
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" }
The information provided in this article is accurate at the time of writing, but on-going development in the open-source projects involved may make the information incorrect or obsolete. Please open issue or contact us on IBM Z Community if you have any questions or feedback.