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

- **ID:** `mongodb/geo-index-coordinates-invalid`
- **领域:** mongodb
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

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

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| MongoDB 4.4 | active | — | — |
| MongoDB 5.0 | active | — | — |
| MongoDB 6.0 | active | — | — |

## 解决方案

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

## 无效尝试

- **** — The index rebuilds from the same invalid data, causing the same error. (90% 失败率)
- **** — The validation is part of the geospatial index specification and is version-independent. (95% 失败率)
- **** — MongoDB does not support custom validation for geospatial index constraints. (80% 失败率)
