2017-03-22 55 views
1

如何使用mongocxx C++驅動程序遞歸生成Mongodb文檔? 1.我使用mongocxx C++驅動程序v.3和C++ 11。 2.這是我的main.cpp方法,它解析十六進制字符串,並生成這樣mongocxx代碼: 控制檯:$ ./main解剖0x160301012c01000128030340c70e243001b96d8c 和輸出:如何使用mongocxx C++驅動程序遞歸生成Mongodb文檔?

<< "MainType" << bsoncxx::builder::stream::open_document 
    << "TLSRecord" << bsoncxx::builder::stream::open_document 
     << "type"<< "16" 
     << "version"<< "0301" 
     << "length"<< "012C" 
     << "hsMsg" << bsoncxx::builder::stream::open_document 
      << "type"<< "01" 
      << "length"<< "000128" 
      << "clientHello" << bsoncxx::builder::stream::open_document 
       << "version"<< "0303" 
       << "random"<< "40C70E243001B96D8C" 
       << "session_id_length"<< "" 
      << bsoncxx::builder::stream::close_document 
     << bsoncxx::builder::stream::close_document 
    << bsoncxx::builder::stream::close_document 
  • 之後,我需要在mongodb中推它。從解剖法形式的main.cpp

  • 在這裏,我被堆疊起來,嘗試編譯時出錯。

    src/MongodbMapper.cpp:76:6: note: candidate function not viable: no known conversion from'bsoncxx::v_noabi::builder::stream::key_context<bsoncxx::v_noabi::builder::stream::key_context<bsoncxx::v_noabi::builder::stream::closed_context> >' to 'bsoncxx::builder::stream::document &' for 3rd argument void generateDocument(DataUnit& node, int level, bsoncxx::builder::stream::document& doc) {

  • 回答

    1

    很難沒有看到您發佈的段的情況下是肯定的,但它看起來像你正在運行到的問題是與<<操作的輸出類型在流建設者。流生成器實際上被錯誤地命名;它在典型的C++意義上並不是一個「流」,因爲<<運算符的輸出類型有時會與左手邊的操作數不同。特別是,無論何時使用類似open_documentclose_document的類型,表達式輸出的類型將與左側的操作數不同。因此,您通常需要存儲其中一個表達式的輸出。

    由於流生成器經常在這種情況下導致混淆,因此通常最好使用基本生成器。儘管基本構建器的語法稍微冗長一些,但要做出微妙的錯誤則更加困難,而當您犯了一個錯誤時,編譯器錯誤消息更容易理解。

    這裏是你如何建立在同一個文檔的基本構造器:

    #include <bsoncxx/builder/basic/document.hpp> 
    #include <bsoncxx/builder/basic/kvp.hpp> 
    #include <bsoncxx/builder/basic/sub_document.hpp> 
    
    using bsoncxx::builder::basic::kvp; 
    using bsoncxx::builder::basic::sub_document; 
    
    bsoncxx::builder::basic::document doc; 
    
    // Build the document 
    doc.append(kvp("MainType", [](sub_document sub_doc1) { 
        sub_doc1.append(kvp("TLSRecord", [](sub_document sub_doc2) { 
         sub_doc2.append(kvp("type", "16"), 
             kvp("version", "0301"), 
             kvp("length", "012C"), 
             kvp("hsMsg", [](sub_document sub_doc3) { 
              sub_doc3.append(kvp("type", "01"), 
                  kvp("length", "000128"), 
                  kvp("clientHello", [](sub_document sub_doc4) { 
                   sub_doc4.append(
                    kvp("version", "0303"), 
                    kvp("random", "40C70E243001B96D8C"), 
                    kvp("session_id_length", "")); 
                  })); 
             })); 
        })); 
    })); 
    
    // Get a view of the document being built and do something with it. 
    do_something_with_document_view(doc.view()); 
    
    // Extract the document from the builder and do something with it. 
    do_something_with_owned_document(doc.extract()); 
    

    bsoncxx::builder::basic::document::append需要的kvp的(鍵值對)一個任意數,並將其追加到建設者。對於像字符串這樣的基本類型,您可以將該值作爲第二個參數傳遞。要建立一個子文檔,使用一個lambda作爲第二個參數,它採用bsoncxx::builder::basic::sub_document,然後以相同的方式附加到該子文檔生成器。

    要從構建器獲取文檔,您可以使用view()extract()方法。view()返回bsoncxx::document::view(),它是文檔的無主視圖;構建器需要在整個視圖使用期間保持活動狀態。 extract()返回一個bsoncxx :: document :: value,它是一個擁有的值;當調用extract()時,構建器將重置爲空狀態。

    相關問題