mongo-c-driver 是 MongoDB 官方的 C++ 语言客户端开发包。
使用示例代码如下:
#include <cstdint> #include <iostream> #include <vector> #include <bsoncxx/json.hpp> #include <mongocxx/client.hpp> #include <mongocxx/stdx.hpp> #include <mongocxx/uri.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; int main() { /* 初始化,创建一个客户端连接 */ mongocxx::instance instance {}; /* This should be done only once. */ mongocxx::uri uri( "mongodb://localhost:27017" ); mongocxx::client client( uri ); /* 访问指定的数据库和集合 */ mongocxx::database db = client["mydb"]; mongocxx::collection coll = db["test"]; /* 创建一个BSON文档 */ /* * { * "name" : "MongoDB", * "type" : "database", * "count" : 1, * "versions": [ "v3.2", "v3.0", "v2.6" ], * "info" : { * "x" : 203, * "y" : 102 * } * } */ 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::stdx::optional<mongocxx::result::insert_one> result = coll.insert_one( doc ); /* * 查询集合中的文档 * 1、查找一个 */ bsoncxx::stdx::optional<bsoncxx::document::value> maybe_result = coll.find_one( document {} << finalize ); if ( maybe_result ) { std::cout << bsoncxx::to_json( *maybe_result ); } /* 2、查找全部 */ mongocxx::cursor cursor = coll.find( document {} << finalize ); for ( auto doc : cursor ) { std::cout << bsoncxx::to_json( doc ) << "\n"; } /* 3、指定过滤条件查询一个 */ bsoncxx::stdx::optional<bsoncxx::document::value> maybe_result = coll.find_one( document {} << "i" << 71 << finalize ); if ( maybe_result ) { std::cout << bsoncxx::to_json( *maybe_result ) << "\n"; } /* 4、获取与筛选器匹配的所有文档 */ mongocxx::cursor cursor = coll.find( document {} << "i" << open_document << "$gt" << 50 << "$lte" << 100 << close_document << finalize ); for ( auto doc : cursor ) { std::cout << bsoncxx::to_json( doc ) << "\n"; } /* * 更新集合中的文档 * 1、更新单个文件 */ coll.update_one( document {} << "i" << 10 << finalize, document {} << "$set" << open_document << "i" << 110 << close_document << finalize ); /* 2、更新多个文档 */ bsoncxx::stdx::optional<mongocxx::result::update> result = coll.update_many( document {} << "i" << open_document << "$lt" << 100 << close_document << finalize, document {} << "$inc" << open_document << "i" << 100 << close_document << finalize ); if ( result ) { std::cout << result->modified_count() << "\n"; } /* * 删除集合中的文档 * 1、删除单个文档 */ coll.delete_one( document {} << "i" << 110 << finalize ); /* 2、删除多个文档 */ bsoncxx::stdx::optional<mongocxx::result::delete_result> result = coll.delete_many( document {} << "i" << open_document << "$gte" << 100 << close_document << finalize ); if ( result ) { std::cout << result->deleted_count() << "\n"; } /* * 创建索引 * 索引结构 { "index1": "<type>", "index2": <type> } * index1、index2为索引的字段 * 对于升序索引,指定<type>为1. * 对于降序索引,指定<type>为-1. */ auto index_specification = document {} << "i" << 1 << finalize; collection.create_index( std::move( index_specification ) ); return(0); }