MongoDB c++ driver(四)
简单的查找实现
#include <iostream>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/options/find.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/uri.hpp>
using bsoncxx::builder::stream::document;
using bsoncxx::builder::stream::open_document;
using bsoncxx::builder::stream::close_document;
using bsoncxx::builder::stream::open_array;
using bsoncxx::builder::stream::close_array;
using bsoncxx::builder::stream::finalize;
int main(int argc, char** args)
{
mongocxx::instance inst{};
mongocxx::client conn{mongocxx::uri{"mongodb://chenzw:IP:端口"}};
auto db = conn["guanwang"];
auto cursor = db["restaurants"].find({});
for (auto&& doc : cursor)
{
std::cout << bsoncxx::to_json(doc) << std::endl;
}
return 0;
}
编译、运行
$ c++ –std=c++11 3find.cpp -o 3find $(pkg-config –cflags –libs libmongocxx)
$ ./3find
运行结果: