2016-11-30 56 views
0

將數組添加到文檔時,open_array和close_array在投入文檔流時必須同時發生。在添加'close_array'時,後面的代碼在最後一行失敗(編譯時間)。MongoDB C++,未能將數組添加到文檔中

vector<string> List; 
... 

document Doc; 
Doc <<"List" <<open_array; 
for (string Str: List) { 
    Doc <<Str; 
} 
Doc <<close_array; 

但我不知道「列表」中要添加到文檔的元素數量。 MongoDB仍然缺少C++驅動程序的示例。

此代碼有效,但「列表」中的項目數量未知。

Doc 
<<open_array 
<<List[0] <<List[1] <<List[2] <<... 
<<close_array; 

G ++錯誤:

content.cpp:65:7: error: no match for ‘operator<<’ (operand types are ‘bsoncxx::v_noabi::builder::stream::document’ and ‘const bsoncxx::v_noabi::builder::stream::close_array_type’) 
    Doc <<close_array; 
    ~~~~^~~~~~~~~~~~~ 
compilation terminated due to -Wfatal-errors. 

回答

0

實測值從Adding a BSON Array to a MongoDB 3.2 document and extracting the values back (MongoCXX 3.2) (C++ 11)

這不是能夠將 'close_array' 添加到文檔本身,它必須通」的陣列構建器被添加(一個溶液鍵入'auto',我沒有挖出來找到真正的類型)。

auto Array = Doc <<"List" <<open_array; 
for (string Str: List) 
    Array <<Str; 
Array <<close_array; 

要注意的是,上面的代碼工作正常,但下面不會

auto Array = Doc <<"List"; 
Array <<open_array; 
for (string Str: List) 
    Array <<Str; 
Array <<close_array; 
+0

你知道,如果在3.0工作嗎?我認爲它不... –

+0

我建立從https://github.com/mongodb/mongo-cxx-driver/archive/r3.1.0.tar.gz的mongodb C++驅動程序3.1.0 – johnlowvale

+0

像在代碼在我的答案工作正常 – johnlowvale

相關問題