我有一个包含两个集合的数据库,如下所示:
[
{
"name": "Person A",
"location": {"type": "Point", "coordinates": [180, 90]}
},
{
"name": "Person B",
"location": {"type": "Point", "coordinates": [-180, -90]}
}
]
[
{
"name": "Store A",
"location": {"type": "Point", "coordinates": [180, 90]}
},
{
"name": "Store B",
"location": {"type": "Point", "coordinates": [-180, -90]}
}
]
对于每个人,我想找到最近的地方。我可以让它找到一对特定坐标的最近位置,而不是整个集合。有没有办法不使用来做到这一点foreach
?这是我在MongoDB 文档之后得到的最接近的结果:
// Current $geoNear:
{
// Instead of having to give constants of a single
// point, I want to compare against all coordinates
// of another collection. ↓
near: {"type":"Point","coordinates":[-180, 90]},
distanceField: 'distance',
maxDistance: 50,
spherical: true
}