查找所有位置$ geoWithin $ geoIntersects

时间:2019-03-08 17:43:24

标签: mongodb

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

我正在使用一个包含餐厅数据(点)的集合,另一个使用附近数据(多边形)的集合

两个集合均由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()

我的问题:

哪个社区的餐馆最多?

0 个答案:

没有答案