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 <iostream>
#include <bsoncxx/builder/stream/document.hpp>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
int main(int, char**) {
mongocxx::instance inst{};
mongocxx::client conn{mongocxx::uri{"mongodb://testuser:testpwd@127.0.0.1:27018/testdb"}};
//注意在Mongodb3.X版本中需要确保testdb存在,并且testuser:testpwd是在testdb库中进行授权的用户
bsoncxx::builder::stream::document document{};
auto collection = conn["testdb"]["testcollection"];
std::cout<<"type:-----------------------------------"<<typeid(collection).name()<<std::endl;
decltype(collection) col=collection;
std::cout<<"type:-----------------------------------"<<typeid(col).name()<<std::endl;
document <<"hello" << "world"
<<"name" <<"csr"
<<"No" <<34
<<"3D" <<bsoncxx::builder::stream::open_array
<<48<<32.123456789<<47
<<bsoncxx::builder::stream::close_array
<<"friends" <<bsoncxx::builder::stream::open_array
<<"zhangsan"<<"lisi"<<"wangwu"
<<bsoncxx::builder::stream::close_array
<<"address0" <<bsoncxx::builder::stream::open_document
<<"city"<<"shenzhen"
<<"nation"<<"china"
<<"phone"<<13524531211
<<bsoncxx::builder::stream::close_document
/*<<bsoncxx::builder::stream::finalize*/;
std::cout<<"the document:"<<bsoncxx::to_json(document) << std::endl;
bsoncxx::builder::stream::document subdocument{};
subdocument << "city" << "sz"<<"nation"<<"china"<<"pos"<<525346;
document<<"address"<<subdocument;
std::cout<<"the document after subdoc:"<<bsoncxx::to_json(document) << std::endl;
//插入文档
collection.insert_one(document.view());
collection.insert_one(document.view());
collection.insert_one(document.view());
//全部罗列文档
auto cursor = collection.find({});
for (auto&& doc : cursor) {
std::cout << bsoncxx::to_json(doc) << std::endl;
}
std::cout<<"\n after insert_one three times count:"<<collection.count({})<<std::endl;
//删除文档
collection.delete_one(document.view());
std::cout<<"\n after delete_one once count:"<<collection.count({})<<std::endl;
// collection.delete_many(document.view());
// std::cout<<"\n after delete_many once count:"<<collection.count({})<<std::endl;
//罗列全部数据库
auto dbcur = conn.list_databases();
for(auto && db:dbcur){
std::cout<<bsoncxx::to_json(db)<<std::endl;
}
std::cout<<std::endl;
std::cout<<std::endl;
// collection =conn.database("eos").collection("transaction_traces");
//查找文档
// cursor = collection.find({});
//按普通key-volue查找
// cursor = collection.find((bsoncxx::builder::basic::make_document(bsoncxx::builder::basic::kvp("actor", "token"))));
// cursor = collection.find((bsoncxx::builder::basic::make_document(bsoncxx::builder::basic::kvp("block_num", 4171818))));
//按完整数组查找
bsoncxx::builder::stream::document finddocument{};
// finddocument<<"3D" <<bsoncxx::builder::stream::open_array
// <<48<<32.123456789<<47
// <<bsoncxx::builder::stream::close_array;
//按数组的一部分内容查找
// finddocument<<"3D" <<48;
//嵌套文档查找
finddocument<<"address0" <<bsoncxx::builder::stream::open_document
<<"city"<<"shenzhen"
<<"nation"<<"china"
<<"phone"<<13524531211
<<bsoncxx::builder::stream::close_document;
cursor = collection.find(finddocument.view());
// bsoncxx::builder::stream::document filter_builder;
// filter_builder << "stime" << open_document
// << "$lt" << bsoncxx::types::b_date(std::chrono::system_clock::now())
// << close_document
// << "stime" << open_document
// << "$gt" << bsoncxx::types::b_date(std::chrono::system_clock::now() - std::chrono::seconds{ 600 })
// << close_document
// << "iscancel" << 0
// << "gid" << strGId;
// //相当于sql语句:SELECT * FROM bak__user_order WHERE gid=“2423seef”
// // and iscancel=0
// // AND add_time < 1514233574
// // and add_time >=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):"<<std::endl<<bsoncxx::to_json(doc) << std::endl;
}
if(flag==0)
std::cout<<"\nnot found!!!"<<std::endl;
std::cout<<"count:"<<collection.count({})<<std::endl;
}