mongodb - $geoNear 用于列表嵌入位置和列表中所有位置的返回距离
问题描述
我有一个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
保持正确的地址有人知道怎么做吗?谢谢
解决方案
您需要将每个地址存储在单独的文档中:
{
"_id" : ObjectId("5ec77d127df107cd889d567d"),
"name" : "David",
"age" : 20,
"addresses" : {
"radius" : 10000,
"location" : {
"type" : "Point",
"coordinates" : [
105.785299,
20.979733
]
}
}
},
{
"_id" : ObjectId("5ec77f7843732e8f9a63bf67"),
"name" : "David",
"age" : 20,
"addresses" : {
"radius" : 30000,
"location" : {
"type" : "Point",
"coordinates" : [
105.795299,
20.989733
]
}
}
}
现在,我们执行$geoNear
和$group
阶段:
db.user.aggregate([
{
"$geoNear": {
"near": {
"type": "Point",
"coordinates": [
105.823620,
21.006047
]
},
"distanceField": "distance",
"key": "addresses.location"
}
},
{
"$group": {
"_id": "$name",
"name": {
"$first": "$name"
},
"age": {
"$first": "$age"
},
"addresses": {
"$push": {
"$mergeObjects": [
"$addresses",
{
"distance": "$distance"
}
]
}
}
}
}
])
推荐阅读
- react-native - react-navigation 使用一些不同的参数推送到同一个屏幕
- reactjs - 在 react-testing-library 中查询 font-awesome 元素
- sql - SQL - 删除多列具有空值或零值的行
- javascript - 有没有可能我在表名 users 上搜索,然后在 mvc 中的用户表名角色列上进行搜索
- jquery - 我正在尝试在 jquery 中添加动态行,但在 for 循环中出现语法错误
- facebook - 将隐私 URL 上传到 developer.facebook.com 时出错
- mysql - 如何从具有 fk 约束的多表中删除?
- android - 如何使用刷新令牌在 aws amplify android 中获取新的 id 令牌?
- javascript - 模态 - JQuery - JS - FullCalendar 上的 Select2 问题
- python - 如何删除 Python “1 of 1 completed”