问题描述
在python中,pymongo为MongoDB GeoSpatial索引提供了很好的支持.但是,对于C ++,当我在C ++中使用mongocxx时,我对语法有些困惑.
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.
例如,在我使用的python(pymongo)中
For example, in python (pymongo) I used
cursor = db.colection.find(
{
"loc": {
"$near": [lon, lat]
}
}
).limit(10)
以获取给定位置的最近10件商品.但是我该如何在C ++中做同样的事情?
to get nearest 10 items for given location. But how can I do the same thing in C++?
我尝试过:
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.
有人可以给我一些有关C ++中的GeoSpatial索引的说明吗?文档/示例将被高度重视.
Could anyone give me some instructions on GeoSpatial index in C++? Documents/examples will be highly apreciated.
非常感谢.
推荐答案
您可以使用 mongocxx::options::find::limit
.还要检查 mongocxx::collection::find
.以下应该工作:
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);
这篇关于如何在C ++中使用MongoDB GeoSpatial索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!