<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          MongoDB C++ DriverMongoDB 的 C++ 客戶端開發(fā)包

          聯(lián)合創(chuàng)作 · 2023-09-30 20:56

          mongo-c-driver 是 MongoDB 官方的 C++ 語(yǔ)言客戶端開發(fā)包。

          使用示例代碼如下:

          #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()
          {
          	/* 初始化,創(chuàng)建一個(gè)客戶端連接 */
          	mongocxx::instance	instance {}; /* This should be done only once. */
          	mongocxx::uri		uri( "mongodb://localhost:27017" );
          	mongocxx::client	client( uri );
          
          	/* 訪問(wèn)指定的數(shù)據(jù)庫(kù)和集合 */
          	mongocxx::database	db	= client["mydb"];
          	mongocxx::collection	coll	= db["test"];
          
          	/* 創(chuàng)建一個(gè)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、查找一個(gè)
          	 */
          	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、指定過(guò)濾條件查詢一個(gè) */
          	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、更新單個(gè)文件
           	*/
          	coll.update_one( document {} << "i" << 10 << finalize,
          			 document {} << "$set" << open_document <<
          			 "i" << 110 << close_document << finalize );
          	/* 2、更新多個(gè)文檔 */
          	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、刪除單個(gè)文檔
          	 */
          	coll.delete_one( document {} << "i" << 110 << finalize );
          	/* 2、刪除多個(gè)文檔 */
          	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";
          	}
          
          	/*
           	* 創(chuàng)建索引
           	* 索引結(jié)構(gòu) { "index1": "<type>", "index2": <type> }
          	 * index1、index2為索引的字段
          	 * 對(duì)于升序索引,指定<type>為1.
          	 * 對(duì)于降序索引,指定<type>為-1.
           	*/
          
          	auto index_specification = document {} << "i" << 1 << finalize;
          	collection.create_index( std::move( index_specification ) );
          
          	return(0);
          }
          瀏覽 23
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          編輯 分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          編輯 分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  国产高清免费视频在线观看一区 | 无码高清操逼 | 370p日韩欧美亚洲精品 | 免费看又黄又无码的网站 | 国产精品久久久久永久免费看 |