无法提取地理位置键,经度/纬度超出范围

wes*_*bos 11 google-maps mongoose mongodb

使用MongoDB 2dsphere存储GEOJSON并在lat/lng的某些商店中我不断收到以下错误:

{
  "code": 16755,
  "index": 0,
  "errmsg": "Can't extract geo keys: { _id: ObjectId('586ff135b79aa00b84181bfb'), name: \"Austin\", slug: \"Austin\", description: \"\", twitter: \"\", facebook: \"\", instagram: \"\", author: ObjectId('57b60fed8620b56af460d5c5'), tags: [], created: new Date(1483731253640), location: { address: \"Austin, TX, United States\", coordinates: [ 30.267153, -97.74306079999997 ], type: \"Point\" }, __v: 0 }  longitude/latitude is out of bounds, lng: 30.2672 lat: -97.7431",
  "op": {
    "name": "Austin",
    "slug": "Austin",
    "description": "",
    "twitter": "",
    "facebook": "",
    "instagram": "",
    "author": "57b60fed8620b56af460d5c5",
    "_id": "586ff135b79aa00b84181bfb",
    "tags": [

    ],
    "created": "2017-01-06T19:34:13.640Z",
    "location": {
      "address": "Austin, TX, United States",
      "coordinates": [
        30.267153,
        -97.74306079999997
      ],
      "type": "Point"
    },
    "__v": 0
  }
}
Run Code Online (Sandbox Code Playgroud)

我似乎无法弄清楚为什么它不喜欢某些lat/lng坐标.

这是我的位置字段架构:

  location: {
    type: { type: String, default: 'Point' },
    coordinates: [Number],
    address: String
  },
Run Code Online (Sandbox Code Playgroud)

它的索引为2dsphere:

storeSchema.index({ location: '2dsphere' });

我能看到的唯一奇怪的是错误信息:

longitude/latitude is out of bounds, lng: 30.2672 lat: -97.7431",

lat/lng比我输入的更短 - 不确定是否与它有任何关系.

wes*_*bos 30

哦哇 - 我不确定是谁决定这个,但Mongodb希望你存储[lng, lat],而不是[lat,lng]像这个世界上的其他东西一样.

:|

  • `[lng,lat]`排序是GeoJSON的标准.通常它更直观,因为`lng`对应于`x`和`lat`对应于2d坐标中的`y` (4认同)

ra9*_*a9r 5

这是我无需交换[lat,lng]序列即可解决问题的方法。

在您的代码中,您正在调用ensureIndex. 而不是打电话(就像我以前做的那样)......

collection.ensureIndex({ "location": "2dsphere" });
Run Code Online (Sandbox Code Playgroud)

改用以下...

collection.ensureIndex({ "location.coordinates":"2d"});
Run Code Online (Sandbox Code Playgroud)

这完全消除了运行时的错误,不需要大量的数据重构。