-
Notifications
You must be signed in to change notification settings - Fork 544
Quickstart Guide (New Driver)
Andrew C. Morrow edited this page Mar 24, 2016
·
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.
- 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.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 and check out the latest stable 1.3.x release:
git clone https://github.com/mongodb/mongo-c-driver
git checkout 1.3.4
- 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] [--with-libbson=bundled] [--enable-ssl] [--enable-sasl] ...
make && sudo make install
- 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)