1

在 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++ 中处理地理空间索引?提前致谢。

4

1 回答 1

2

您可以使用与 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));

}

于 2017-04-07T21:46:57.923 回答