我有一个看起来像这样的集合:
Mongodb 采集数据:
/* 1 */
{
"_id" : "X1255",
"member" : {
"id" : "X1255",
"address" : [
{
"place" : "TEST1",
"location" : {
"type" : "Point",
"coordinates" : [
-83.66403,
31.02459
]
}
},
{
"place" : "TEST2",
"location" : {
"type" : "Point",
"coordinates" : [
-83.61883,
31.54664
]
}
}
]
}
}
/* 2 */
{
"_id" : "X30127",
"member" : {
"id" : "X30127",
"address" : [
{
"place" : "TEST3",
"location" : {
"type" : "Point",
"coordinates" : [
-83.61883,
31.54664
]
}
}
]
}
}
member.address.location.coordinates 上有一个 2dsphere 索引。
Mongod geonear 查询的可选参数只返回一个坐标值。即使收集第一条和第二条记录具有相同的坐标值,查询也只返回一个子文档。但预期的结果是,当集合数据中有多个匹配坐标时,查询应该给出所有子文档。
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",
"member" : {
"id" : "X1255",
"address" : [
{
"place" : "TEST1",
"location" : {
"type" : "Point",
"coordinates" : [
-83.66403,
32.02459
]
}
}
]
}
}
/* 2 */
{
"_id" : "X30127",
"member" : {
"id" : "X30127",
"address" : [
{
"place" : "TEST3",
"location" : {
"type" : "Point",
"coordinates" : [
-83.61883,
31.54664
]
}
}
]
}
}
预期结果:
/* 1 */
{
"_id" : "X1255",
"member" : {
"id" : "X1255",
"address" : [
{
"place" : "TEST1",
"location" : {
"type" : "Point",
"coordinates" : [
-83.66403,
32.02459
]
}
}
]
}
}
/* 2 */
{
"_id" : "X1255",
"member" : {
"id" : "X1255",
"address" : [
{
"place" : "TEST2",
"location" : {
"type" : "Point",
"coordinates" : [
-83.61883,
31.54664
]
}
}
]
}
}
/* 3 */
{
"_id" : "X30127",
"member" : {
"id" : "X30127",
"address" : [
{
"place" : "TEST3",
"location" : {
"type" : "Point",
"coordinates" : [
-83.61883,
31.54664
]
}
}
]
}
}