我正在使用react geosuggest包来保存MongoDB中我的事件的位置。该软件包从Google场所获取数据,每次我存储它时,它都会像这样存储:
"_id": "vvagbSbEginyrQGK8",
"name": "test3",
"googleLocation": {
"label": "Testaccio, Roma, Italia",
"placeId": "ChIJE47T5i5gLxMRhiCxCUAFq4Q",
"isFixture": false,
"gmaps": {
"address_components": [
{
"long_name": "Monte Testaccio",
"short_name": "Monte Testaccio",
"types": [
"establishment",
"natural_feature"
]
},
{
"long_name": "Rome",
"short_name": "Rome",
"types": [
"locality",
"political"
]
},
{
"long_name": "Rome",
"short_name": "Rome",
"types": [
"administrative_area_level_3",
"political"
]
},
{
"long_name": "Metropolitan City of Rome",
"short_name": "RM",
"types": [
"administrative_area_level_2",
"political"
]
},
{
"long_name": "Lazio",
"short_name": "Lazio",
"types": [
"administrative_area_level_1",
"political"
]
},
{
"long_name": "Italia",
"short_name": "IT",
"types": [
"country",
"political"
]
},
{
"long_name": "00153",
"short_name": "00153",
"types": [
"postal_code"
]
}
],
"formatted_address": "Monte Testaccio, 00153 Rome, Italia",
"geometry": {
"location": {},
"location_type": "APPROXIMATE",
"viewport": {
"f": {
"b": 41.87460301970851,
"f": 41.87730098029151
},
"b": {
"b": 12.474345019708494,
"f": 12.477042980291571
}
}
},
"place_id": "ChIJE47T5i5gLxMRhiCxCUAFq4Q",
"types": [
"establishment",
"natural_feature"
]
},
"location": {
"lat": 41.87595200000001,
"lng": 12.475693999999976
}
}
我已经尝试了很多不同的mongodb查询查询,但我无法弄明白。任何人都知道一个查询,例如根据我发送的经度和纬度查找10000米范围内的所有文件。
答案 0 :(得分:2)
根据mongodb的当前版本,它有Gospatial查询运算符,您可以在修改包含以下位置的集合后实现所需:
首先,您应该为集合创建“2dsphere”索引,让我们将其命名为“places”,如下所示:
db.places.createIndex( { loc : "2dsphere" } );
/*
the document should contain "loc" property as following:
{
loc : { type: "Point", coordinates: [ -73.88, 40.78 ] },
//+ the other needed properties.
}
*/
然后,对于特定的原点和距离范围,您可以应用以下查询:
db.places.find(
{
loc:
{ $near :
{
$geometry: { type: "Point", coordinates: [ -73.9667, 40.78 ]
//the origin point.
},
$minDistance: 1000, //in meter
$maxDistance: 5000 //in meter
}
}
}
)
请注意,坐标的顺序如下[long,lat]。
有关详细信息,您可以在以下链接中查看mongodb文档: