一个mongo-cxx的Demo(makefile编译)

 

 

makefile

复制代码
INC_DIR=-I/opt/mongo-cxx-driver/include/mongocxx/v_noabi/  -I/opt/mongo-cxx-driver/include/bsoncxx/v_noabi/

LIB_DIR=-L/opt/mongo-cxx-driver/lib64/

LIB=-lmongocxx -lbsoncxx 

CC=g++ -g
CFLAGS=-Wall -std=c++11
EXE=test

all:
    $(CC) $(CFLAGS) $(EXE).cpp -o $(EXE) $(INC_DIR) $(LIB_DIR) $(LIB)

clean:
    rm -rf *.o $(EXE)
    
复制代码

 

test.cpp

复制代码
#include <iostream>

#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>

#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>

//## #不要在root下编译任何代码(因为root的环境变量和非root不同。当前配置的环境变量,都是在非root账号的)
// c++ --std=c++11 test.cpp -o test  -I/opt/mongo-cxx-driver/include/mongocxx/v_noabi   -I/opt/mongo-cxx-driver/include/bsoncxx/v_noabi   -L/opt/mongo-cxx-driver/lib64 -lmongocxx -lbsoncxx

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;
    }
}
复制代码

 

posted @   He_LiangLiang  阅读(445)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示