首页 > 解决方案 > 查找所有位置 $geoWithin $geoIntersects

问题描述

我想找出集合中的哪个多边形包含另一个集合中的最多点。

我正在使用一个包含餐厅数据(点)的集合和一个包含邻域数据(多边形)的集合

这两个集合都由 mongodb 提供:

https://raw.githubusercontent.com/mongodb/docs-assets/geospatial/neighborhoods.json https://raw.githubusercontent.com/mongodb/docs-assets/geospatial/restaurants.json

邻里文件:

{
    "_id": {
        "$oid": "55cb9c666c522cafdb053a1a"
    },
    "geometry": {
        "coordinates": [
            [
                [
                    -73.9443878859649,
                    40.70042452378256
                ],
                [
                    -73.94424286147482,
                    40.69969927964773
                ], 
[
                    -73.94193078816193,
                    40.70072523469547
                ],…
            ]
        ],
        "type": "Polygon"
    },
    "name": "Bedford"
}

餐厅文件

{
"_id" : { "$oid" : "55cba2476c522cafdb053add" },
"location" : {"coordinates":[-73.856077,40.848447] , "type":"Point" },
"name" : "Morris Park Bake Shop"
}

这是一个查找单个区域内所有餐厅的示例:

通过给定点($geoIntersects)选择一个邻域(多边形)

var neighborhood = db.neighborhoods.findOne( 
{ 
    geometry: 
    { 
     $geoIntersects: 
     { 
      $geometry: { type: "Point", coordinates: [ -73.93414657, 40.82302903 ] } 
     } 
    } 
} 
)

找出附近有多少家餐馆

db.restaurants.find( { location: { $geoWithin: { $geometry: neighborhood.geometry } } } ).count()

我的问题:

哪个街区的餐馆最多?

标签: mongodb

解决方案


推荐阅读