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