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

查看:48
本文介绍了$ 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

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天全站免登陆