MongoDB中的地理空间查询返回包含指定点(经度,纬度)的文档

时间:2014-12-18 22:35:54

标签: mongodb geospatial nosql

我正在尝试创建一个查询,该查询将返回所有具有多边形或LineString的文档,其中包含经度,纬度坐标系上的点。任何人都可以帮我确定我的查询问题吗?我试图解决这个问题很长一段时间,我的查询似乎遵循mongo 2.6.5文档。

到目前为止,考虑供应商集合中的文档可能定义如下:

{ "VENDOR": "LUIGIS HARDWARE STORE", "AREA_ON_EARTH_THAT_SHOP_SERVICES": { "type": "LineString", "coordinates": [ [ -73.987792968875, 40.728235015886916 ], [ -73.98737335280508, 40.71806335844922 ], [ -73.986816840625, 40.72778488158918 ], [ -73.98597718728516, 40.71747858911133 ], [ -73.98331451418602, 40.726640847241214 ], [ -73.98307800292969, 40.71710968017578 ], [ -73.9828872680664, 40.71734619140625 ], [ -73.98235321044922, 40.718345642089844 ], [ -73.98203751708984, 40.729120025634764 ], [ -73.98183781738281, 40.719478607177734 ], [ -73.9813332421875, 40.720550537109375 ], [ -73.98103095458984, 40.72093963623047 ], [ -73.9805679321289, 40.72159957885742 ], [ -73.98014068603516, 40.72217559814453 ], [ -73.97969055175781, 40.722801208496094 ], [ -73.97838592529297, 40.72460174560547 ], [ -73.97750091552734, 40.72579574584961 ], [ -73.97659301757812, 40.72700500488281 ], [ -73.97615814208984, 40.7276725769043 ], [ -73.97564697265625, 40.72834014892578 ], [ -73.9739761352539, 40.734655670166016 ], [ -73.9738998413086, 40.73478918457031 ], [ -73.97396087646484, 40.7410791015625 ], [ -73.9742660522461, 40.732242584228516 ], [ -73.97443389892578, 40.732810974121094 ], [ -73.97505950927734, 40.735130310058594 ], [ -73.9751205444336, 40.735748291015625 ], [ -73.9751205444336, 40.73612976074219 ], [ -73.97500610351562, 40.73664855957031 ], [ -73.97478485107422, 40.737037658691406 ], [ -73.97380828857422, 40.73845291137695 ], [ -73.97335815429688, 40.73912811279297 ], [ -73.9732437133789, 40.73942947387695 ], [ -73.97313690185547, 40.73974609375 ], [ -73.97337341308594, 40.73988342285156 ], [ -73.97344970703125, 40.7398681640625 ], [ -73.97357940673828, 40.73987579345703 ], [ -73.97401428222656, 40.74009704589844 ], [ -73.97419738769531, 40.740135192871094 ], [ -73.97447967529297, 40.74012756347656 ], [ -73.97579956054688, 40.74070358276367 ], [ -73.98030090332031, 40.742591857910156 ], [ -73.9834213256836, 40.74393081665039 ], [ -73.9886245727539, 40.74612045288086 ], [ -73.9901123046875, 40.74671173095703 ], [ -73.99053955078125, 40.74614334106445 ], [ -73.99099731445312, 40.7454948425293 ], [ -73.99236297607422, 40.7436408996582 ], [ -73.99419403076172, 40.741111755371094 ], [ -73.9954833984375, 40.739356994628906 ], [ -73.99685668945312, 40.73744583129883 ], [ -73.99828338623047, 40.7354850769043 ], [ -74.0000991821289, 40.74301315307617 ], [ -74.0005874633789, 40.74229217529297 ], [ -74.0009994506836, 40.74176956176758 ], [ -74.00108337402344, 40.74163986206055 ], [ -74.00177001953125, 40.7305908203125 ], [ -74.00286865234375, 40.72846984863281 ], [ -74.00321197509766, 40.72770690917969 ], [ -74.00350952148438, 40.72700500488281 ], [ -74.00405883789062, 40.72560119628906 ], [ -74.00466918945312, 40.724159240722656 ], [ -74.0048828125, 40.72370529174805 ], [ -74.00494384765625, 40.723575592041016 ], [ -74.00322723388672, 40.72327423095703 ], [ -74.00308227539062, 40.7332666015625 ], [ -74.00252532958984, 40.72303009033203 ], [ -73.9991455078125, 40.721378326416016 ], [ -73.99333679199219, 40.72102355957031 ], [ -73.99387902832031, 40.721527099609375 ], [ -73.9933373413086, 40.72134780883789 ], [ -73.99390356445312, 40.72053527832031 ], [ -73.99302673339844, 40.71986770629883 ], [ -73.99363110351562, 40.7191276550293 ], [ -73.98379296875, 40.71823501586914 ] ] } }

鉴于此,我正在尝试以下查询失败:

db.vendors.find({ AREA_ON_EARTH_THAT_SHOP_SERVICES : { $geoWithin: { $geometry: { type: "Point", coordinates: [ -73.997439,40.730823]
} } } })

导致错误:

error: { "$err" : "$within not supported with provided geometry: { $geoWithin: { $geometry: { type: \"Point\", coordinates: [ -73.997439,40.730823 ] } } }", "code" : 16672 }

2 个答案:

答案 0 :(得分:1)

形状运算符不能是“点”。可用的形状运算符是:$ box,$ polygon,$ center和$ centerSphere。

在这种情况下你可以使用$ center:

{
   "AREA_ON_EARTH_THAT_SHOP_SERVICES": {
      $geoWithin: { $center: [ [ <x>, <y> ] , <radius> ] }
   }
}

你的查询应该是这样的,请注意10是使圆心从中心覆盖的半径完全是多边形:

db.vendors.find({ "AREA_ON_EARTH_THAT_SHOP_SERVICES" : 
{ $geoWithin: { $center: [ [ -73.98652, 40.752044 ], 10 ] } }
})

或者您可以将$ geoIntersects与“Point”一起使用,如果这样可以获得预期的结果:

db.vendors.find({ "AREA_ON_EARTH_THAT_SHOP_SERVICES" : { $geoIntersects: 
{ $geometry: { type: "Point", coordinates: [ -73.98652, 40.752044 ]
} } } })

答案 1 :(得分:0)

我几乎遇到了同样的问题,并通过以下方式得到了一个可行的例子:

db.getCollection('places').insert({
  id: 'some square from 0,0 to 10,10',
  loc: {
     type: "Polygon",
     coordinates: [ [ [ 0 , 0 ] , [ 0 , 10 ] , [ 10 , 10 ] , [ 10 , 0 ], [ 0 , 0  ] ] ]
  }
})

然后通过loc字段创建索引:

db.getCollection('places').createIndex( { loc : "2dsphere" } )

,然后使用$geoIntersects$geometry键入Point

db.getCollection('places').find(
   {
     loc: {
       $geoIntersects: {
          $geometry: {
             type : "Point" ,
             coordinates:
               [
                 1, 1
               ]

          }
       }
     }
   }
)