-
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.9+. - 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 1.3 branch.
git clone -b master https://github.com/mongodb/mongo-c-driver
-
Build and install the C driver. If you don't need to override the C driver's default install path, you can omit the --prefix argument.
cd mongo-c-driver
./autogen.sh [--prefix=CDRIVER-INSTALL-PATH]
make && sudo make install
- Clone the repository.
git clone -b master https://github.com/mongodb/mongo-cxx-driver
- 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=/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{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:
c++ --std=c++11 hellomongo.cpp -o hellomongo $(pkg-config --cflags --libs libmongocxx)