C++连接MongoDB的增删查改

1、前置条件需要安装好MongoDB的驱动(mongocxx),mongo跑起来。

2、项目包含相关头文件和库,例如:

INCLUDEPATH += "/usr/local/include/mongocxx/v_noabi"
INCLUDEPATH += "/usr/local/include/bsoncxx/v_noabi"

LIBS += -L/usr/local/lib/ -lbsoncxx  -lmongocxx

3、C++代码如下,已经很精简,不废话解释了:


#include 
#include 
#include 
#include 
#include 

int main(int, char**) {
    mongocxx::instance inst{};
    mongocxx::client conn{mongocxx::uri{"mongodb://testuser:[email protected]:27018/testdb"}};
//注意在Mongodb3.X版本中需要确保testdb存在,并且testuser:testpwd是在testdb库中进行授权的用户
    bsoncxx::builder::stream::document document{};
    auto collection = conn["testdb"]["testcollection"];
    std::cout<<"type:-----------------------------------"<=1514233574



    int flag=0;

    for(auto&& doc : cursor/*collection.find(bsoncxx::builder::basic::make_document(bsoncxx::builder::basic::kvp("block_num", 3120204)))*/){

//        bsoncxx::to_json(doc);
        std::cout<<++flag<<"(find):"<

 

你可能感兴趣的:(MongoDB)