mongocxx基本增删改查操作
公用事业费
mongodb 的C++驱动分为mongocxx和legacy(旧版本)两个版本。2个版本的驱动安装和API都不相同。现在仅介绍mongocxx版本的操作。
#include <iostream>
#include <bsoncxx/json.hpp>
#include <mongocxx/client.hpp>
#include <mongocxx/stdx.hpp>
#include <mongocxx/uri.hpp>
#include<mongocxx/instance.hpp>
using bsoncxx::builder::stream::clo_array;
using bsoncxx::builder::stream::clo_document;
using bsoncxx::builder::stream::document;
using bsoncxx::builder::stream::finalize;
using bsoncxx::builder::stream::open_array;
usingbsoncxx::builder::stream::open_document;
mongocxx::instance instance{}; // This should be doneonly once.
mongocxx::uri uri("mongodb://localhost:27018");
mongocxx::client client(uri);
黄山英语mongocxx::databa db = client["mydb"];//获取数据库“mydb”,如果client是指针类型,则可以通过databa()⽅法获取。
偶数是什么//获取集合
mongocxx::collection coll = db["test"];//获取test集合
auto builder =bsoncxx::builder::stream::document{};
bsoncxx::document::value doc_value = builder
<< "name" << "MongoDB"
<< "type" << "databa"
<< "count" << 1
<< "versions" <<bsoncxx::builder::stream::open_array
魔鬼营业员
<< "v3.2" << "v3.0" << "v2.6"
<< clo_array梦见抓到很多鱼
<< "info" <<bsoncxx::builder::stream::open_document
<< "x"<< 203
<< "y" << 102
<<bsoncxx::builder::stream::clo_document
<< bsoncxx::builder::stream::finalize;
/
/插⼊⼀条数据
coll.inrt_one(doc_value.view());
//删除⼀条数据
coll.delete_one(document{}<<" name "<<"MongoDB"<<
吸氧机有用吗
" type "<< " databa "<<finalize);
//更新⼀条数据
collSiaNeName.update_one(document{}<<
" name "<<"MongoDB" <<finalize,
document{}<<"$t"<<open_document
<<"type"<<"data"
<<clo_document
<<finalize);
//查询⼀个集合
mongocxx::stdx::optional<bsoncxx::document::value> result = coll.find_one(document{} <<" name"<<"MongoDB"<<finalize);
if(result){
bsoncxx::document::view view = (*result).view();
std::string Name = view["name"].get_utf8()._string(); //获取查询到的name的值,若是double类型则为view["name"].get_double();
}
//建⽴索引
垃圾分类是什么酒后吃什么好