-
Notifications
You must be signed in to change notification settings - Fork 544
Quickstart Guide (New Driver)
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.
- Windows or any standard 'nix platform.
- A modern compiler. We require Clang 3.5+, Apple Clang 5.1+, GCC 4.8.2+, or VC2015 Update 1+.
- CMake 3.2+.
- The MongoDB C driver version 1.3.5+.
The C++ driver uses libbson and the MongoDB C driver internally. If you don't already have a new enough version of the C driver and libbson installed, then you need to build them.
Build the C driver according to the section "Building From a Release Tarball" in the install instructions. The C driver installs libbson if necessary.
-
Clone the repository, and check out the latest stable release
git clone -b master https://github.com/mongodb/mongo-cxx-driver
git checkout r3.0.0
-
Build the driver. Note that if you installed the cdriver to a path that is automatically searched by pkgconfig, you can omit the PKG_CONFIG_PATH environment variable.
cd mongo-cxx-driver/build
[PKG_CONFIG_PATH=CDRIVER_INSTALL_PATH/lib/pkgconfig] cmake -DCMAKE_BUILD_TYPE=Release [-DCMAKE_INSTALL_PREFIX=CDRIVER-INSTALL-PATH] ..
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{mongocxx::uri{}};
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:
[PKG_CONFIG_PATH=CDRIVER_INSTALL_PATH/lib/pkgconfig] c++ --std=c++11 hellomongo.cpp -o hellomongo $(pkg-config --cflags --libs libmongocxx)