【发布时间】:2020-05-22 15:40:59
【问题描述】:
我有一个user 收藏:
{
"name": "David",
"age": 20,
"addresses": [
{
"radius": 10000,
"location": {
"type": "Point",
"coordinates": [106.785299, 20.999999]
}
},
{
"radius": 30000,
"location": {
"type": "Point",
"coordinates": [105.785299, 20.979733]
}
}
]
}
每个用户都有一个或多个地址。我想用一个点计算这些地址之间的距离,然后使用计算出的距离与每个地址的半径进行比较。如果距离 addresses 列表中删除地址。我正在使用以下查询:
db.collection.aggregrate(
{
"$geoNear": {
"near": {"type": "Point", "coordinates": [ 105.823620, 21.006047 ]},
"distanceField": "distance",
"key": "addresses.location"
}
}
)
但是这个查询只返回最近地址的距离,像这样:
{
"name": "David",
"age": 20,
"addresses": [
{
"radius": 10000,
"location": {
"type": "Point",
"coordinates": [105.785299, 20.979733]
}
},
{
"radius": 30000,
"location": {
"type": "Point",
"coordinates": [105.785299, 20.979733]
}
}
],
"distance": 110000 // <--- distance is added here, just for nearest addrest
}
我的预期结果:
{
"name": "David",
"age": 20,
"addresses": [
{
"radius": 10000,
"location": {
"type": "Point",
"coordinates": [105.785299, 20.979733]
},
"distance": 2000``// <------ add distance here for each addesss`
},
{
"radius": 30000,
"location": {
"type": "Point",
"coordinates": [105.785299, 20.979733]
},
"distance": 30000 // <------ add distance here for each addesss
}
]
}
所以下一阶段我可以比较 distance 和 radius 并保持正确的地址
有人知道怎么做吗?谢谢
【问题讨论】:
-
两个地址的坐标相同,所以它们的距离是相同的。您需要存储 1 个地址 1 个文档,然后
$geoNear可以正确计算您的期望值。然后,您可以将它们分组到所需的输出中 -
@Valijon,抱歉,这只是一个示例数据。当然,所有地址都会有不同的坐标。我已经解决了我的问题
标签: mongodb aggregation-framework