MongoDB 使用C++ driver 3.1.1 使用案例1

来源:互联网 发布:代运营淘宝如何收费用 编辑:程序博客网 时间:2024/04/30 02:45
#include <cstdint>#include <iostream>#include <vector>#include <bsoncxx/json.hpp>#include <mongocxx/client.hpp>#include <mongocxx/stdx.hpp>#include <mongocxx/uri.hpp>#include <mongocxx/instance.hpp>using bsoncxx::builder::stream::close_array;using bsoncxx::builder::stream::close_document;using bsoncxx::builder::stream::document;using bsoncxx::builder::stream::finalize;using bsoncxx::builder::stream::open_array;using bsoncxx::builder::stream::open_document;#pragma comment(lib, "bsoncxx.lib")#pragma comment(lib, "mongocxx.lib")#pragma comment(lib, "bson-1.0.lib")#pragma comment(lib, "mongoc-1.0.lib")int main(){mongocxx::instance instance = {};mongocxx::uri uri("mongodb://127.0.0.1:27017");mongocxx::client client(uri);mongocxx::database db = client["mydb"];db.create_collection("www");mongocxx::collection coll = db["test"];auto builder = bsoncxx::builder::stream::document{};bsoncxx::document::value doc_value = builder<< "name" << "MongoDB"<< "type" << "database"<< "count" << 1<< "versions" << bsoncxx::builder::stream::open_array<< "v3.2" << "v3.0" << "v2.6"<< close_array<< "info" << bsoncxx::builder::stream::open_document<< "x" << 203<< "y" << 102<< bsoncxx::builder::stream::close_document<< bsoncxx::builder::stream::finalize;bsoncxx::document::view view = doc_value.view();bsoncxx::document::element element = view["name"];if (element.type() != bsoncxx::type::k_utf8) {// Error}std::string name = element.get_utf8().value.to_string();}

0 0
原创粉丝点击