mongodb data_error ai_generated true

MongoServerError: 无法提取地理键:无效的坐标值:纬度超出范围 [-90, 90]

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

ID: mongodb/geo-index-coordinates-invalid

其他格式: JSON · Markdown 中文 · English
85%修复率
83%置信度
1证据数
2023-04-10首次发现

版本兼容性

版本状态引入弃用备注
MongoDB 4.4 active
MongoDB 5.0 active
MongoDB 6.0 active

根因分析

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

English

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

generic

官方文档

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

解决方案

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

无效尝试

常见但无效的做法:

  1. 90% 失败

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

  2. 95% 失败

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

  3. 80% 失败

    MongoDB does not support custom validation for geospatial index constraints.