$geoNear(聚合管道)未返回正确的文档 [英] $geoNear (aggregate pipeline) not returning correct documents

查看:16
本文介绍了$geoNear(聚合管道)未返回正确的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am not getting the correct results returned when using $geoNear in the aggregate pipeline. The same query using a typical find() query (using $near) does in fact return the right results.

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

BUT, when removing the equality condition (on schedule.key), both queries return the correct data.

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

$near 查找查询:

$near find query:

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

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

A document in this collection looks something like this:

{
    "_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"
}

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

I have created the appropriate index on the point field:

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

推荐答案

这根本不是相同"的查询.使用单独的 $match 有明显的区别 阶段,因为过滤"仅在找到"最近的结果"之后才进行.这意味着您可能会返回更少"的结果,因为标准不是组合发布的.

It's not the "same" query at all. There is a distinct difference in using a separate $match stage, since the "filtering" is only done "after" the "nearest resuts" are found. This means that you potentially return "less" results since the criteria is not issued in combination.

这就是为什么在 选项的原因">$geoNear:

That's why there is a "query" option in $geoNear:

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

现在是同一个查询.或者如果您使用 $nearSphere.由于 $near 不考虑距离计算中的地球曲率.$nearSphere$geoNear 可以.

Now that's the same query. Or it would be exactly the same if you used $nearSphere. Since $near does not account for the curvature of the earth in distance calcuations. $nearSphere and $geoNear does.

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

But the main point is combining with the "query" option, since that's the only way you truly get both criteria considered in the initial search.

这篇关于$geoNear(聚合管道)未返回正确的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆