【问题标题】:[bsoncxx ]How to append a bsoncxx::document::element TO bsoncxx::builder::basic::document?[bsoncxx ]如何将 bsoncxx::document::element 添加到 bsoncxx::builder::basic::document?
【发布时间】:2017-08-02 03:35:47
【问题描述】:

尝试将元素附加到文档时出现错误。

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
}

错误:错误 C2664 'void bsoncxx::v_noabi::builder::basic::sub_document::append_(bsoncxx::v_noabi::builder::concatenate_doc)': 无法从 'bsoncxx::v_noabi::document::element' 转换参数 1 到 'bsoncxx::v_noabi::builder::concatenate_doc'

请帮我解决这个问题,如何将元素转换为文档或将元素附加到文档。

谢谢

【问题讨论】:

    标签: c++ mongodb bson mongo-cxx-driver


    【解决方案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()));
    

    【讨论】:

      【解决方案2】:

      我认为您正在尝试创建此 JSON 结构:

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

      如果我将此结构与您的代码进行比较,您将缺少key2。尝试使用辅助函数kvp() (Key-Value-Pair)..


      附上一个使用多边形创建地理空间查询的小示例。

      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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-02-26
        • 2019-03-04
        • 2017-03-11
        • 1970-01-01
        • 1970-01-01
        • 2020-12-27
        • 2016-10-22
        相关资源
        最近更新 更多