How to create GeoSpatial index in MongoDB using C++ -
in python/pymongo, creating geospatial index quite trivial:
db.collection.create_index([("loc", geo2d)], min=-100, max=100) after can insert data using "loc" field.
but in c++/mongocxx, after referring mongocxx document (http://mongodb.github.io/mongo-cxx-driver/mongocxx-v3/tutorial/) , geospatial docement, still cannot figure out how this.
could kindly show me how deal geospatial index in c++? in advance.
you can create geospatial index c++ driver in similar way python driver; main difference instead of passing minimum , maximum direct arguments create_index, they're instead set in options::index object passed create_index. here's short program creates index described above using c++ driver:
#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)); }
Comments
Post a Comment