MongoDB C++ Driver setup document

 

Useful Resources:

 

http://mongodb.github.io/mongo-cxx-driver/

https://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/installation/

 

 

Required packages :

 

[ root @ DBVersity : /home/mongod ] ll -lhtr

drwxr-xr-x  6 root   root   4.0K Aug  4 17:12 cmake-3.9.0-Linux-x86_64

-rw-r–r–  1 root   root   4.4K Aug  9 07:18 CMakeLists.txt

drwxr-xr-x  9   1000 comp   4.0K Aug 10 09:12 libbson-1.7.0

drwxrwxr-x 11 root   root   4.0K Aug 17 13:14 mongo-cxx-driver-r3.1.2

drwxrwxr-x  9   1000 comp   4.0K Aug 23 06:42 boost_1_61_0

drwxr-xr-x 10 root   root   4.0K Aug 24 07:46 backups

drwxr-xr-x 10   1000 comp   4.0K Aug 24 08:57 mongo-c-driver-1.7.0

[ root @ DBVersity : /home/mongod ]

 

Environment variables to set:

 

[ root @ DBVersity : /home/mongod ] cat ~/.bashrc

export PATH=$PATH:/home/mongod/cmake-3.9.0-Linux-x86_64/bin:/xenv/gcc_gnu/X/5.4.0/bin:/home/mongod/mongo-cxx-driver-r3.1.2/build/src/mongocxx/config/:/home/mongod/mongo-c-driver-1.6.3/src/libbson:/opt/mongodb/bin/

export CMAKE_C_COMPILER=/xenv/gcc_gnu/X/5.4.0/bin/gcc

export CMAKE_CXX_COMPILER=/xenv/gcc_gnu/X/5.4.0/bin/g++

export CMAKE_INSTALL_PREFIX=/home/mongod/mongo-cxx-driver-r3.1.2/src/mongocxx/

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig

export LD_LIBRARY_PATH=/usr/local/lib/

[ root @ DBVersity : /home/mongod ] source ~/.bashrc

 

 

Installation process step by step

 

Download below packages from respective websites from below and configure them in the PATH as show above.

 

yum install pkgconfig

 

mongo-c-driver-1.7.0 : https://github.com/mongodb/mongo-c-driver/releases/download/1.7.0/mongo-c-driver-1.7.0.tar.gz

 

mongo-cxx-driver-r3.1.2 : https://github.com/mongodb/mongo-cxx-driver/archive/r3.1.2.tar.gz

 

boost_1_61_0 : http://www.boost.org/doc/libs/1_61_0/more/getting_started/unix-variants.html#id25

 

cmake-3.9.0-Linux-x86_64

libbson-1.7.0

 

 

 

Step 1: Install the latest version of the MongoDB C driver.

 

$ yum install pkgconfig

$ curl -LO https://github.com/mongodb/mongo-c-driver/releases/download/1.7.0/mongo-c-driver-1.7.0.tar.gz

$ tar xzf mongo-c-driver-1.7.0.tar.gz

$ cd mongo-c-driver-1.7.0

$ ./configure

$ make

$ sudo make install

 

 

Step 2: Choose a C++17 polyfill

 

Build and installation procedure without access to the github repository from vm, use the boost C++ newest version library. You will need to use at least version 1.56.

 

After downloading the source code from this link, use the following set of commands:

http://www.boost.org/doc/libs/1_61_0/more/getting_started/unix-variants.html#id25

 

 

./bootstrap.sh –prefix=/usr/local/

./b2 install

 

This will build and install the boost library to the /usr/local/ directory.

 

 

Step 3: Download the latest version of the mongocxx driver.

 

$ curl -OL https://github.com/mongodb/mongo-cxx-driver/archive/r3.1.2.tar.gz

$ tar -xzf r3.1.2.tar.gz

$ cd mongo-cxx-driver-r3.1.2/build

 

Step 4: Configure the driver.

 

cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local -DCMAKE_CXX_COMPILER=/xenv/gcc_gnu/X/5.4.0/bin/g++ -DCMAKE_C_COMPILER=/xenv/gcc_gnu/X/5.4.0/bin/gcc  -DBSONCXX_POLY_USE_BOOST=1 -DLIBBSON_DIR=/usr/local/ -DLIBMONGOC_DIR=/usr/local/ ..

 

Step 5: Build and install the driver.

 

make && sudo make install

 

Add “ #include <assert.h> “  in /home/mongod/mongo-cxx-driver-r3.1.2/src/mongocxx/test_util/mock.hh if it complains with below error

 

/home/mongod/mongo-cxx-driver-r3.1.2/src/mongocxx/test_util/mock.hh:173:9: error:

      use of undeclared identifier ‘assert’

        assert(!current);  // It is impossible to create two instances in a single thread

 

Step 6: Test your installation.

 

 

Create below test connect.cpp program file  in src folder i.e., /home/mongod/mongo-cxx-driver-r3.1.2/src

 

#include <cstdlib>

#include <iostream>

#include <string>

#include <vector>

 

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

#include <bsoncxx/builder/core.hpp>

#include <bsoncxx/json.hpp>

#include <bsoncxx/stdx/make_unique.hpp>

 

#include <mongocxx/client.hpp>

#include <mongocxx/instance.hpp>

#include <mongocxx/logger.hpp>

#include <mongocxx/options/client.hpp>

#include <mongocxx/uri.hpp>

#include <mongocxx/stdx.hpp>

 

using bsoncxx::builder::stream::close_array;

using bsoncxx::builder::stream::close_document;

using bsoncxx::builder::stream::document;

using bsoncxx::builder::stream::finalize;

using bsoncxx::builder::stream::open_array;

using bsoncxx::builder::stream::open_document;

 

namespace {

 

class logger final : public mongocxx::logger {

   public:

    explicit logger(std::ostream* stream) : _stream(stream) {

    }

 

    void operator()(mongocxx::log_level level, mongocxx::stdx::string_view domain,

                    mongocxx::stdx::string_view message) noexcept override {

        if (level >= mongocxx::log_level::k_trace) return;

        *_stream << ‘[‘ << mongocxx::to_string(level) << ‘@’ << domain << “] ” << message << ‘\n’;

    }

 

   private:

    std::ostream* const _stream;

};

 

}  // namespace

 

int main(int argc, char* argv[]) {

    using bsoncxx::builder::stream::document;

 

    mongocxx::instance inst{bsoncxx::stdx::make_unique<logger>(&std::cout)};

 

    try {

        const auto uri = mongocxx::uri{(argc >= 2) ? argv[1] : mongocxx::uri::k_default_uri};

 

        mongocxx::options::client client_options;

        if (uri.ssl()) {

            mongocxx::options::ssl ssl_options;

            client_options.ssl_opts(ssl_options);

        }

 

        auto client = mongocxx::client{uri, client_options};

 

        auto admin = client[“admin”];

 

        document ismaster;

        ismaster << “isMaster” << 1;

 

        auto result = admin.run_command(ismaster.view());

 

        std::cout << bsoncxx::to_json(result) << “\n”;

 

        return EXIT_SUCCESS;

 

    } catch (const std::exception& xcp) {

        std::cout << “connection failed: ” << xcp.what() << “\n”;

        return EXIT_FAILURE;

    }

}

 

Export below env variables if not done in first step

 

export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig/

export LD_LIBRARY_PATH=/usr/local/lib/

 

Replace the libstdc++.so.6 with /usr/lib64/

 

mv /xenv/gcc_gnu/Y/5.4.0/lib64/libstdc++.so.6 /usr/lib64/

 

Compile it the connect.cpp as below.

 

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

 

Try connect as below.

 

./connect

 

You should see output as below.

 

 [ root @ DBVersity : /home/mongod/mongo-cxx-driver-r3.1.2/src ]

[ root @ DBVersity : /home/mongod/mongo-cxx-driver-r3.1.2/src ] c++ –std=c++11 connect.cpp -o connect $(pkg-config –cflags –libs libmongocxx)

 

[ root @ DBVersity : /home/mongod/mongo-cxx-driver-r3.1.2/src ] ./connect

[info@mongocxx] libmongoc logging callback enabled

{ “ismaster” : true, “maxBsonObjectSize” : 16777216, “maxMessageSizeBytes” : 48000000, “maxWriteBatchSize” : 1000, “localTime” : { “$date” : 1503584435808 }, “maxWireVersion” : 5, “minWireVersion” : 0, “readOnly” : false, “ok” : 1.0 }

[ root @ DBVersity : /home/mongod/mongo-cxx-driver-r3.1.2/src ]

  • Ask Question