mongodb data_error ai_generated true

MongoServerError: Can't extract geo keys: invalid coordinate value: latitude out of range [-90, 90]

ID: mongodb/geo-index-coordinates-invalid

Also available as: JSON · Markdown · 中文
85%Fix Rate
83%Confidence
1Evidence
2023-04-10First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
MongoDB 4.4 active
MongoDB 5.0 active
MongoDB 6.0 active

Root Cause

A document with a 2dsphere index contains a coordinate with latitude outside the valid range of -90 to 90 degrees.

generic

中文

具有 2dsphere 索引的文档包含一个纬度超出 -90 到 90 度有效范围的坐标。

Official Documentation

https://www.mongodb.com/docs/manual/geospatial-queries/

Workarounds

  1. 70% success Query for documents with invalid coordinates using $geoNear with a large radius to identify them, then update or delete: db.places.find({ loc: { $geoWithin: { $centerSphere: [[0, 0], 3.14159] } } })
    Query for documents with invalid coordinates using $geoNear with a large radius to identify them, then update or delete: db.places.find({ loc: { $geoWithin: { $centerSphere: [[0, 0], 3.14159] } } })
  2. 85% success Use an aggregation pipeline to find documents where loc.coordinates[1] is outside [-90, 90] and fix them: db.places.aggregate([ { $match: { $expr: { $or: [ { $lt: [ { $arrayElemAt: ["$loc.coordinates", 1] }, -90 ] }, { $gt: [ { $arrayElemAt: ["$loc.coordinates", 1] }, 90 ] } ] } } } ])
    Use an aggregation pipeline to find documents where loc.coordinates[1] is outside [-90, 90] and fix them: db.places.aggregate([ { $match: { $expr: { $or: [ { $lt: [ { $arrayElemAt: ["$loc.coordinates", 1] }, -90 ] }, { $gt: [ { $arrayElemAt: ["$loc.coordinates", 1] }, 90 ] } ] } } } ])
  3. 90% success Drop the 2dsphere index, bulk update all documents to clamp coordinates to valid ranges, then recreate the index: db.places.dropIndex("loc_2dsphere"); db.places.updateMany({}, [{ $set: { "loc.coordinates.1": { $max: [ { $min: [ "$loc.coordinates.1", 90 ] }, -90 ] } } } ]); db.places.createIndex({ loc: "2dsphere" })
    Drop the 2dsphere index, bulk update all documents to clamp coordinates to valid ranges, then recreate the index: db.places.dropIndex("loc_2dsphere"); db.places.updateMany({}, [{ $set: { "loc.coordinates.1": { $max: [ { $min: [ "$loc.coordinates.1", 90 ] }, -90 ] } } } ]); db.places.createIndex({ loc: "2dsphere" })

中文步骤

  1. Query for documents with invalid coordinates using $geoNear with a large radius to identify them, then update or delete: db.places.find({ loc: { $geoWithin: { $centerSphere: [[0, 0], 3.14159] } } })
  2. Use an aggregation pipeline to find documents where loc.coordinates[1] is outside [-90, 90] and fix them: db.places.aggregate([ { $match: { $expr: { $or: [ { $lt: [ { $arrayElemAt: ["$loc.coordinates", 1] }, -90 ] }, { $gt: [ { $arrayElemAt: ["$loc.coordinates", 1] }, 90 ] } ] } } } ])
  3. Drop the 2dsphere index, bulk update all documents to clamp coordinates to valid ranges, then recreate the index: db.places.dropIndex("loc_2dsphere"); db.places.updateMany({}, [{ $set: { "loc.coordinates.1": { $max: [ { $min: [ "$loc.coordinates.1", 90 ] }, -90 ] } } } ]); db.places.createIndex({ loc: "2dsphere" })

Dead Ends

Common approaches that don't work:

  1. 90% fail

    The index rebuilds from the same invalid data, causing the same error.

  2. 95% fail

    The validation is part of the geospatial index specification and is version-independent.

  3. 80% fail

    MongoDB does not support custom validation for geospatial index constraints.