您的位置:首页 > 数据库 > Mongodb

MongoDB c++ driver(五)

2016-07-03 15:05 295 查看

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:123@IP:端口"}};

auto db = conn["guanwang"];

//finds documents whose borough field equals "Manhattan"
auto cursor = db["restaurants"].find(document{} << "borough" << "Manhattan" << finalize);

for (auto&& doc : cursor) //打印出"guanwang"下"restaurants"所有文档
{
std::cout << bsoncxx::to_json(doc) << std::endl;
}

return 0;
}


编译、运行

$ c++ –std=c++11 3find2.cpp -o 3find2 $(pkg-config –cflags –libs libmongocxx)

$./3find2

运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: