Unable to retrieve geo-keys, longitude / latitude out of bounds

Working with MongoDB 2dsphere to store GEOJSON and in some lat / lng stores. I keep getting the following error:

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

I can't understand why he doesn't like lat / lng coordinates.

Here is my diagram for the location field:

  location: { type: { type: String, default: 'Point' }, coordinates: [Number], address: String }, 

and indexed as 2dsphere :

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

The only thing I see is the error message:

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

lat / lng are shortened from what I entered - not sure if this has anything to do with it.

+5
source share
2 answers

Oh wow, I'm not sure who decided this, but Mongodb expects you to save [lng, lat] , not [lat,lng] , like everything else in this world.

: |

+10
source

This is how I fixed the problem without having to change the sequence [lat,lng] .

Somewhere in your code you call ensureIndex . Instead of calling (as I already did) ...

 collection.ensureIndex({ "location": "2dsphere" }); 

Use the following instead ...

 collection.ensureIndex({ "location.coordinates":"2d"}); 

This completely eliminated the runtime error and did not require massive data refactoring.

+2
source

Source: https://habr.com/ru/post/1262441/


All Articles