How to use MongoDB GeoSpatial Index in C++

核能气质少年 提交于 2019-12-12 23:26:59

问题


In python, pymongo provides nice support for MongoDB GeoSpatial index. However, for C++ when I use mongocxx in C++, I am a little bit confused about the grammar.

For example, in python (pymongo) I used

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

to get nearest 10 items for given location. But how can I do the same thing in C++?

I tried:

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

I am not sure if this is correct approach, and I failed to set the number of results.

Could anyone give me some instructions on GeoSpatial index in C++? Documents/examples will be highly apreciated.

Thank you very much.


回答1:


You can use mongocxx::options::find::limit. Check also mongocxx::collection::find. The following should work :

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);


来源:https://stackoverflow.com/questions/43243200/how-to-use-mongodb-geospatial-index-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!