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

运行结果:

这里写图片描述

Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐