如何在C++中使用MongoDB GeoSpatial Index

How to use MongoDB GeoSpatial Index in C++

本文关键字:MongoDB GeoSpatial Index C++      更新时间:2023-10-16

在python中,pymongo为MongoDB GeoSpatial index提供了很好的支持。但是,C++当我在C++中使用mongocxx时,我对语法有点困惑。

例如,在python(pymongo(中,我使用了

cursor = db.colection.find(
    {
        "loc": {
            "$near": [lon, lat]
        }
    }
).limit(10)

以获取给定位置最近的 10 个项目。但是我怎样才能在C++做同样的事情呢?

我试过了:

mongocxx::cursor cursor = coll.find(document{} << "loc" << open_document <<
                                    "$near" << [lon, lat]
                                    << close_document << finalize);

不确定这是否是正确的方法,并且我未能设置结果的数量。

谁能给我一些关于C++地理空间索引的说明?文件/示例将受到高度赞赏。

谢谢。

您可以使用

mongocxx::options::find::limit .另请检查mongocxx::collection::find 。以下应该有效:

mongocxx::options::find opts;
opts.limit(10);
mongocxx::cursor cursor = coll.find(document{} << "loc" << open_document 
    << "$near" << bsoncxx::builder::stream::open_array 
    << lon << lat << bsoncxx::builder::stream::close_array 
    << close_document << finalize, opts);