【问题标题】:How to create GeoSpatial index in MongoDB using C++如何使用 C++ 在 MongoDB 中创建地理空间索引
【发布时间】:2017-04-07 19:10:57
【问题描述】:

在 python/pymongo 中,创建 GeoSpatial 索引非常简单:

db.collection.create_index([("loc", GEO2D)], min=-100, max=100)

之后我可以使用“loc”字段插入数据。

但是在 C++/mongocxx 中,在参考了 mongocxx 文档 (http://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/tutorial/) 和 GeoSpatial 文档后,我仍然无法弄清楚如何做到这一点。

谁能告诉我如何在 C++ 中处理地理空间索引? 提前致谢。

【问题讨论】:

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


    【解决方案1】:

    您可以使用与 Python 驱动程序类似的方式使用 C++ 驱动程序创建 GeoSpatial 索引;主要区别在于,不是将最小值和最大值作为直接参数传递给create_index,而是将它们设置在options::index 对象中,然后将其传递给create_index。这是一个使用 C++ 驱动程序创建上述索引的简短程序:

    #include <bsoncxx/builder/basic/document.hpp>
    #include <bsoncxx/builder/basic/kvp.hpp>
    #include <mongocxx/client.hpp>
    #include <mongocxx/instance.hpp>
    #include <mongocxx/options/index.hpp>
    #include <mongocxx/uri.hpp>
    
    using namespace mongocxx;
    using bsoncxx::builder::basic::kvp;
    
    int main() {
        instance inst{};
    
        client conn{uri{}};
        auto coll = conn["db_name"]["coll_name"];
    
        bsoncxx::builder::basic::document index_doc;
        index_doc.append(kvp("loc", "2d"));
    
        coll.create_index(
            index_doc.extract(),
            options::index{}
                .twod_location_min(-100).twod_location_max(100));
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-02
      • 1970-01-01
      • 2011-10-23
      • 2017-02-02
      • 2016-02-12
      • 2011-12-03
      • 1970-01-01
      • 2014-01-04
      相关资源
      最近更新 更多