一个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 @ 2020-05-20 09:35  He_LiangLiang  阅读(443)  评论(0编辑  收藏  举报