“MongoError: Can't extract geo keys from object” with Type : Point

前端 未结 6 1026
伪装坚强ぢ
伪装坚强ぢ 2020-12-15 19:28

I try to prepare my database field for geocoding with this:

MyCollection._ensureIndex({\'data.address.located\':\'2dsphere\'});

But then th

相关标签:
6条回答
  • I got the same error, but the issue was I had a LineString with two identical coordinates.

    "geometry" : {
        "type" : "LineString",
        "coordinates" : [ 
            [ 
                143.345763, 
                -33.840952
            ], 
            [ 
                143.345763, 
                -33.840952
            ]
        ]
    }
    

    I had to make this into a Point for it to be valid

    "geometry" : {
        "type" : "Point",
        "coordinates" : [ 
            143.345763, 
            -33.840952
        ]
    }
    
    0 讨论(0)
  • 2020-12-15 19:53

    As mentioned by @go-oleg the problem is that range of coordinate are next:

    • Longitude: (+-) 180°
    • Latitude: (+-) 90°

    and index expects coordinates to be in that bounds.

    But if you are trying to apply index on already imported data and found out that coordinates are swapped, you'll probably want to swap it back rather than reimporting whole collection. For this purpose you can use next mongoshell script.

    Assuming that you already have well-formed GeoJSON of type Point.

    db.coll.find().forEach(function (e) {
        // assuming that `geoJson` is our field containing `coordinates`
        e.geoJson.coordinates = [ // Swapping Lat/Lon
            e.geoJson.coordinates[1], // Longitude goes first
            e.geoJson.coordinates[0] // Latitude goes last
        ];
        db.coll.save(e);
        // Some `print(e.name)` can go here just to understand the progress
    });
    
    0 讨论(0)
  • 2020-12-15 19:56

    The problem is that

    [ 32.4586858, -110.8571443 ]
    

    is not a valid coordinate. The order should be longitude followed by latitude whereas that coordinate appears to be the reverse (judging by the fact that the valid latitude range is -90 to 90 and -110.8571443 is outside of that).

    I think you meant:

    { type: "Point", coordinates: [ -110.8571443, 32.4586858 ] }
    

    or there was some other entry error.

    0 讨论(0)
  • 2020-12-15 20:02

    Even the problem was solved, I need to highlight this bug also may appear if you omit the type of GeoJSON object.

    In my case, it was corrected adding the type: 'Point' to model's value.

    0 讨论(0)
  • 2020-12-15 20:10

    I had the same problem while my longitude, latitude order was correct. My mistake was that I had written coordiantes instead of coordinates the mongo gave me this:

    pymongo.errors.WriteError: Can't extract geo keys: {data}  Point must be an array or object
    

    so maybe you have the right ordering but you have problem in your key names.

    0 讨论(0)
  • 2020-12-15 20:10

    first step is to insert some data that should be in first object format otherwise this errors comes in picture

    schema :
    location:{
                type: { type: String },
                coordinates:[mongoose.Schema.Types.Mixed]
            },
    

    then insert some data into db

    db.userDetails.createIndex({location: "2dsphere"})
    

    index only be created if location contains object

    after that write query ,if u want to findout distance

    db.userDetails.aggregate( [
     $geoNear: {
                      near: { type: "Point",  coordinates: [data.lat1,data.lon1]},
                      spherical: true,
                      "distanceMultiplier" : 0.001,
                      distanceField: "calcDistance",
                      $maxDistance: distance,
    
                   }
    ] )
    

    and one more thing data should be pass without quoated string

    "lat":18.7323
    "lon":73.1232 
    

    like this

    0 讨论(0)
提交回复
热议问题