1

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 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.