Skip to content

Quickstart Guide (New Driver)

Adam Midvidy edited this page Apr 1, 2015 · 34 revisions

The goal of this guide is to get you to the point where you can build a simple application with the driver in a few short minutes.

Prerequisites

  1. *nix platform. We have only tested the driver on Linux and OSX.
  2. A modern compiler. We recommend clang++ 3.4+ or g++ 4.8+.
  3. CMake 3.1+.

Build and install the C driver

The C++ driver uses libbson and the MongoDB C driver internally. The C driver will install libbson if it isn't already present.

  • Install the C driver prerequisites automake, autoconf and libtool. See the C-driver README for further instructions if you run into trouble.

  • Clone the C driver. Currently we depend on APIs in the 1.2.0-dev branch.

git clone -b 1.2.0-dev https://github.com/mongodb/mongo-c-driver

  • Build and install the C driver.

cd mongo-c-driver

./autogen.sh

make && sudo make install

Build and install the C++ driver

  • Clone the repository.

git clone -b master https://github.com/mongodb/mongo-cxx-driver

  • Build the driver.

cd mongo-cxx-driver/build

cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=/usr/local ..

sudo make && sudo make install

  • Fire up your editor and copy in this code to a file called hellomongo.cpp:
#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{};

    bsoncxx::builder::stream::document document{};

    auto collection = conn["testdb"]["testcollection"];
    document << "hello" << "world";

    collection.insert_one(document.view());
    auto cursor = collection.find({});
    
    for (auto&& doc : cursor) {
        std::cout << bsoncxx::to_json(doc) << std::endl;
    }
}

You can compile with:

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