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 (将#修改为@)