首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >$near会返回MultiPoint中任何一点都在范围内的文档吗?

$near会返回MultiPoint中任何一点都在范围内的文档吗?
EN

Stack Overflow用户
提问于 2015-11-18 15:57:00
回答 1查看 998关注 0票数 9

当在一个$near/$geoNear/$geoWithin对象上使用MultiPoint GeoJson对象时,如果点的任何都在该范围内,还是将返回文档,或者它们是否必须在所有的范围内?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-11-19 05:24:30

“近”案

考虑的距离总是来自存储的任何GeoJSON对象的“最近”点。对于多边形,或者MultiPolygon,以及所有对存储有效的GeoJSON对象,也是如此。

考虑到这一点:

代码语言:javascript
复制
{
    "location": {
       "type": "MultiPoint",
       "coordinates": [
          [ -73.9580, 40.8003 ],
          [ -73.9498, 40.7968 ],
          [ -73.9737, 40.7648 ],
          [ -73.9814, 40.7681 ]
       ]
    }
}

如果我们使用聚合$geoNear作为显示与给定位置的距离的一种方法:

代码语言:javascript
复制
db.geo.aggregate([
    { "$geoNear": {
        "near": {
            "type": "Point",
            "coordinates": [
               -73.97661209106445,
               40.774561857347244
            ]
        },
        "spherical": true,
        "distanceField": "distance"
    }}
])

这告诉我们距离被认为是824米。

现在,如果在集合中将每个"Point“视为自己的文档,并运行相同的查询过程:

代码语言:javascript
复制
{
        "location" : {
                "type" : "Point",
                "coordinates" : [
                        -73.9814,
                        40.7681
                ]
        },
        "distance" : 824.837276194968
}
{
        "location" : {
                "type" : "Point",
                "coordinates" : [
                        -73.9737,
                        40.7648
                ]
        },
        "distance" : 1114.0666715946495
}
{
        "location" : {
                "type" : "Point",
                "coordinates" : [
                        -73.958,
                        40.8003
                ]
        },
        "distance" : 3266.4720692258156
}
{
        "location" : {
                "type" : "Point",
                "coordinates" : [
                        -73.9498,
                        40.7968
                ]
        },
        "distance" : 3351.9091229713567
}

然后,您可以看到从原点到每个点的不同距离是查询,在前一种情况下,实际上只考虑整个对象的“最近”。

因此,有证据表明,使用$near/$geoNear或始终只考虑的距离--与查询中使用的原点最近的

$geoWithin案例

然而,$geoWithin操作是不同的。考虑一下orginial "MultiPoint“文档,然后是这个查询:

代码语言:javascript
复制
db.geo.find({
    "location": {
        "$geoWithin": {
            "$geometry": {
                "type": "Polygon",
                "coordinates": [
                  [
                    [
                      -73.98382186889648,
                      40.75961056635002
                    ],
                    [
                      -74.00030136108398,
                      40.782751138401245
                    ],
                    [
                      -73.97317886352539,
                      40.78950978441435
                    ],
                    [
                      -73.95910263061523,
                      40.7720918760227
                    ],
                    [
                      -73.98382186889648,
                      40.75961056635002
                    ]
                  ]
                ]
            }
        }
    }
})

这将不会返回任何结果,也不会因为对象的“并非所有”Point组件都位于查询中使用的tge多边形的范围内。但是,如果您将每一点视为一个单一的文档:

代码语言:javascript
复制
{
        "_id" : ObjectId("564d5efd9f28c6e0feabcef8"),
        "location" : {
                "type" : "Point",
                "coordinates" : [
                        -73.9737,
                        40.7648
                ]
        }
}
{
        "_id" : ObjectId("564d5efd9f28c6e0feabcef9"),
        "location" : {
                "type" : "Point",
                "coordinates" : [
                        -73.9814,
                        40.7681
                ]
        }
}

然后其中的两个点将被认为是在多边形内。但是,由于这些文件不是作为单独的文档存储的,而是作为"MutiPoint“的一部分存储的,所以除非将该对象的所有部分包含在形状中,否则结果是假的,文档也不会被返回。

对于在某些表示中本质上包含"Point“集合的所有GeoJSON对象,这里也是如此。

票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33784631

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档