-
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.
- *nix platform. We have only tested the driver on Linux and OSX.
- A modern compiler. We recommend
clang++
3.4+ org++
4.8+. - CMake 3.1+.
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
andlibtool
. 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
- 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)