微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

MongoDB - $geoNear 和 includeLocs 在同一文档中有多个匹配项

如何解决MongoDB - $geoNear 和 includeLocs 在同一文档中有多个匹配项

我有一个看起来像这样的集合:

Mongodb 集合数据:

/* 1 */
{
    "_id" : "X1255","member" : {
        "id" : "X1255","address" : [ 
            {
                "place" : "TEST1","location" : {
                    "type" : "Point","coordinates" : [ 
                        -83.66403,31.02459
                    ]
                }
            },{
                "place" : "TEST2","coordinates" : [ 
                        -83.61883,31.54664
                    ]
                }
            }
        ]
    }
}

/* 2 */
{
    "_id" : "X30127","member" : {
        "id" : "X30127","address" : [ 
            {
                "place" : "TEST3",31.54664
                    ]
                }
            }
        ]
    }
}

member.address.location.coordinates 上有一个 2dsphere 索引。

Mongod geonear 查询的可选参数只返回一个坐标值。即使 thoung 集合 1st 和 2nd 记录具有相同的坐标值,查询也只返回一个子文档。但预期的结果是当集合数据中有多个匹配坐标时,查询应该给出所有子文档。

Mongodb 查询

db.getCollection('test').aggregate([
{"$geoNear":{"near":{"type":"Point","coordinates":[-83.672608,32.0390586]},"maxdistance":160934,"distanceField":"dist.distance","includeLocs":"dist.locs","spherical":true}},{"$unwind":"$member.address"},{"$project":{"member":{"$cond":[{"$and":[
{"$eq":["$member.address.location.coordinates","$dist.locs.coordinates"]}]},"$member",[]]}}}
])

实际结果:

/* 1 */
{
    "_id" : "X1255",32.02459
                    ]
                }
            }
        ]
    }
}


/* 2 */
{
    "_id" : "X30127",31.54664
                    ]
                }
            }
        ]
    }
}

预期结果:

/* 1 */
{
    "_id" : "X1255",32.02459
                    ]
                }
            }
        ]
    }
}

/* 2 */
{
    "_id" : "X1255","address" : [ 
            {
                "place" : "TEST2",31.54664
                    ]
                }
            }
        ]
    }
}

/* 3 */
{
    "_id" : "X30127",31.54664
                    ]
                }
            }
        ]
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。