2017-07-31 15 views
0

要素を文書に追加しようとするとエラーが発生します。[bsoncxx] bsoncxx :: builder :: basic :: documentにbsoncxx :: document ::要素を追加する方法

bsoncxx::document::value _obj; //This is Declaration of _obj in diffrent file 

bsoncxx::document::element element = _obj.view()[sFieldName]; 
if (element.length() && element.type() == bsoncxx::type::k_document) 
{ 
    bsoncxx::builder::basic::document bsonBuilder; 
    bsonBuilder.append(element); //Getting Error 
} 

Error: Error C2664 'void bsoncxx::v_noabi::builder::basic::sub_document::append_(bsoncxx::v_noabi::builder::concatenate_doc)': cannot convert argument 1 from 'bsoncxx::v_noabi::document::element' to 'bsoncxx::v_noabi::builder::concatenate_doc'

文書化する要素を文書または追加する要素を変換する方法、この問題を解決するために私を助けてください。

おかげで

答えて

1

私はあなたがこのJSON構造を作成しようとしていると思う:私はあなたのコードには、この構造を比較すると、あなたがkey2が欠落している

{ 
    "key1": "value1", 
    "key2": 
    { //this is your sub-document... 
     "subkey1": "subvalue1", 
     "subkey2": "subvalue2" 
    } 
} 


は、ポリゴンを使用して地理空間クエリを作成するために、少し例を添付..ヘルパー機能 kvp()(キー値ペア)を使用してみてください。

using bsoncxx::builder::basic::sub_document; 
using bsoncxx::builder::basic::sub_array; 
using bsoncxx::builder::basic::kvp; 

bsoncxx::builder::basic::document doc{}; 
doc.append(kvp("info.location",[a_polygon](sub_document subdoc) { 
    subdoc.append(kvp("$geoWithin", [a_polygon](sub_document subdoc2) 
    { 
     subdoc2.append(kvp("$geometry", [a_polygon](sub_document subdoc3) 
     { 
      subdoc3.append(kvp("type","Polygon")); 
      subdoc3.append(kvp("coordinates",[a_polygon](sub_array subarray) 
      { 
       subarray.append([a_polygon](sub_array subarray2)   
       { 
        for (auto point : a_polygon->points()) 
        { 
         subarray2.append([point](sub_array coordArray) 
         { 
          coordArray.append(point.longitude(), point.latitude()); 
         }); 
        } 
       }); 
      })); 
     }));   
    })); 
})); 

クエリ構造:

{ 
    <location field>: { 
     $geoWithin: { 
     $geometry: { 
      type: <"Polygon" or "MultiPolygon"> , 
      coordinates: [ <coordinates> ] 
     } 
     } 
    } 
} 

出典:MongoDB Reference

1

ビルダーに要素を追加するには、あなたがbsoncxx::builder::basic::kvpを使用して、個別の要素からキーと値を渡す必要があります:

using bsoncxx::builder::basic::kvp; 

bsoncxx::document::element elem = ...; 
bsoncxx::builder::basic::document builder; 
builder.append(kvp(elem.key(), elem.get_value()));