问题
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