【发布时间】:2016-09-20 01:12:21
【问题描述】:
我的mongo版本是3.2.6
我的桌子是 restaurant_locations。
我给出的索引如下:
db.restaurant_locations._ensureIndex({'location': '2dsphere'});
此表中存储的数据如下:
> db.restaurant_locations.find().pretty();
{
"_id" : ObjectId("573ff477df3c12bdd6463d07"),
"location" : {
"type" : "Point",
"coordinates" : [
72.5220332,
23.0656791
]
}
}
{
"_id" : ObjectId("573ff5badf3c12bdd6463d08"),
"location" : {
"type" : "Point",
"coordinates" : [
62.5220332,
13.0656791
]
}
}
现在当我触发以下查询时:
db.restaurant_locations.find({
location: {
$near: {
$geometry: {
type: 'Point',
coordinates: [ 70, 20 ]
},
$maxDistance: 100000000
}
}
});
我得到两条记录作为输出:
{ "_id" : ObjectId("573ff477df3c12bdd6463d07"), "location" : { "type" : "Point", "coordinates" : [ 72.5220332, 23.0656791 ] } }
{ "_id" : ObjectId("573ff5badf3c12bdd6463d08"), "location" : { "type" : "Point", "coordinates" : [ 62.5220332, 13.0656791 ] } }
现在,当我执行以下查询时:
db.restaurant_locations.find({
location: {
$near: {
$geometry: {
type: 'Point',
coordinates: [ 70, 20 ]
},
$maxDistance: 1000000
}
}
});
我只得到一条记录作为输出:
{ "_id" : ObjectId("573ff477df3c12bdd6463d07"), "location" : { "type" : "Point", "coordinates" : [ 72.5220332, 23.0656791 ] } }
- 我不知道 $maxDistance 是如何工作的。
- 我想找10公里范围内的餐厅,具体是什么,需要传入$maxDistance?
- 假设我的坐标是 40 和 20,那么如果我想要 10 公里内的餐馆,会显示以上两条记录吗?
我参考了许多 mongodb 文档、stackoverflow 问题,但并没有得到确切的想法,它是如何工作的
提前感谢您的帮助
【问题讨论】:
标签: mongodb geolocation location mongodb-query