$geoNear(聚合管道)未返回正确的文档

2023-12-08

使用时我没有得到正确的结果$geoNear在聚合管道中。使用典型的 find() 查询的相同查询(使用$near)实际上确实返回了正确的结果。

BUT,当删除相等条件时(在schedule.key),两个查询都返回正确的数据。

$geoNear使用聚合管道:

db.place.aggregate(
[
    { 
        $geoNear: { 
            spherical: true,
            near: { type: "Point", coordinates: [ 18.416145, -33.911973 ] },
            distanceField: "dist"
        }
    },
    { 
        $match: { 
            "schedule.key": { "$eq": "vo4lRN_Az0uwOkgBzOERyw" } 
        } 
    }
])

$near查找查询:

db.place.find(
    { 
        "point" : { 
            $near: { 
                type: "Point", 
                coordinates: [ 18.416145,-33.911973 ] 
            } 
        }, 
        "schedule.key" : { 
            $eq : "vo4lRN_Az0uwOkgBzOERyw" 
        }
    })

该集合中的文档如下所示:

{
    "_id" : UUID("da6ccbb1-3c7a-45d7-bc36-a5e6007cd919"),
    "schedule" : {
        "_id" : UUID("587de5b7-a744-4b28-baa8-e6efb5f7f921"),
        "key" : "vo4lRN_Az0uwOkgBzOERyw"
    },
    "point" : {
        "type" : "Point",
        "coordinates" : [ 
            18.425102, 
            -33.922153
        ]
    },
    "name" : "Cape Town"
}

我已经在点字段上创建了适当的索引:

db.place.ensureIndex( { "point" : "2dsphere" } );

这根本不是“相同”的查询。使用单独的方法有明显的区别$match阶段,因为“过滤”仅在找到“最近的结果”“之后”完成。这意味着您可能会返回“更少”的结果,因为条件不是组合发出的。

这就是为什么有一个"query"选项中$geoNear:

db.place.aggregate(
[
    { 
        $geoNear: { 
            spherical: true,
            near: { type: "Point", coordinates: [ 18.416145, -33.911973 ] },
            distanceField: "dist",
            query: {
                "schedule.key": { "$eq": "vo4lRN_Az0uwOkgBzOERyw" } 
            }
        }
    }
])

现在这是相同的查询。或者如果你使用的话它会完全相同$nearSphere. Since $near在距离计算中没有考虑地球的曲率。$nearSphere and $geoNear does.

但重点是结合"query"选项,因为这是您真正在初始搜索中考虑这两个条件的唯一方法。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

$geoNear(聚合管道)未返回正确的文档 的相关文章

随机推荐