在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++中处理地理空间索引?提前谢谢。
发布于 2017-04-08 05:46:58
您可以使用C++驱动程序以类似于Python驱动程序的方式创建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));}
https://stackoverflow.com/questions/43285481
复制相似问题