Mongo $ near是否会返回MultiPoint中任意点在范围内的文档? [英] Will a Mongo $near return documents for which any point in a MultiPoint is within range?

查看:102
本文介绍了Mongo $ near是否会返回MultiPoint中任意点在范围内的文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MultiPoint GeoJson对象上使用$near/$geoNear/$geoWithin时,如果任何个点在该范围内,将返回文档,还是全部必须在范围内吗?

When using $near/$geoNear/$geoWithin on a MultiPoint GeoJson object, will the document be returned if any of the points is within the range, or do they all have to be in range?

推荐答案

近"的情况

所考虑的距离将始终是从存储的任何GeoJSON对象的最近"点开始.对于Polygon或MultiPolygon以及实际上对存储有效的所有GeoJSON对象也是如此.

Case for "near"

The distance considered will always be from the "nearest" point of any GeoJSON Object stored. The same goes for Polygon, or MultiPolygon and really all GeoJSON objects that are valid for storage.

考虑一下:

{
    "location": {
       "type": "MultiPoint",
       "coordinates": [
          [ -73.9580, 40.8003 ],
          [ -73.9498, 40.7968 ],
          [ -73.9737, 40.7648 ],
          [ -73.9814, 40.7681 ]
       ]
    }
}

如果我们使用聚合 $geoNear 告诉我们到指定位置的距离:

And if we use aggregation $geoNear as a means to show us the distance from a given location:

db.geo.aggregate([
    { "$geoNear": {
        "near": {
            "type": "Point",
            "coordinates": [
               -73.97661209106445,
               40.774561857347244
            ]
        },
        "spherical": true,
        "distanceField": "distance"
    }}
])

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

This tels us that the distance is considered to be 824 meters.

现在,如果您将每个点"视为它自己的文档而不是集合中的文件,并运行相同的查询过程:

Now if you considered each "Point" as it's own document instead in the collection, and ran the same query process:

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

然后您会看到查询到每个点到原点的距离不同,在前一种情况下,整个对象实际上只考虑了最近".

Then you see the different distances of each point from the point of origin is the query, where in the former case only the "nearest" was actually considered for the whole object.

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

So there is the proof, that the distance considered with $near/$geoNear or is always only the nearest point to the origin used in the query.

$geoWithin操作与此不同.考虑原始的"MultiPoint"文档,然后执行以下查询:

The $geoWithin operation is however different. Consider the orginial "MultiPoint" document and then this query:

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 Polygon的范围内.但是,如果您将每个点都视为一个文档:

This will not return any result, and it will not because "not all" Point components of the Object lie within the bounds of tge Polygon used in the query. But if you considered each point as a single document:

{
        "_id" : ObjectId("564d5efd9f28c6e0feabcef8"),
        "location" : {
                "type" : "Point",
                "coordinates" : [
                        -73.9737,
                        40.7648
                ]
        }
}
{
        "_id" : ObjectId("564d5efd9f28c6e0feabcef9"),
        "location" : {
                "type" : "Point",
                "coordinates" : [
                        -73.9814,
                        40.7681
                ]
        }
}

然后将看到两个点在多边形内部.但是由于这些不是作为单独的文档而是作为"MutiPoint"的一部分存储的,因此,除非该对象的所有部分都包含在形状中,否则结果为false,并且不会返回该文档.

Then two of the points would be seen to be inside the Polygon. But since these are not stored as individual documents but as part of a "MutiPoint", then unless all parts of that Object are contained within the shape, then the result is false and the document is not returned.

对于所有本质上以某种表示形式包含点"的集合的所有GeoJSON对象,这里也是如此.

The same is true here for all GeoJSON Objects that essentially contain a collection of "Point" in some representation.

这篇关于Mongo $ near是否会返回MultiPoint中任意点在范围内的文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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