# MongoServerError: 无法提取地理键：2dsphere 索引需要 'Point'、'MultiPoint'、'LineString'、'MultiLineString'、'Polygon' 或 'MultiPolygon' GeoJSON 类型，但找到 'GeometryCollection'

- **ID:** `mongodb/geo-index-2dsphere-unsupported-type`
- **领域:** mongodb
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 92%

## 根因

2dsphere 索引无法直接索引 GeoJSON GeometryCollection；必须将其分解为单独的几何对象。

## 版本兼容性

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

## 解决方案

1. ```
   将 GeometryCollection 转换为文档中的单个几何对象数组：将字段更新为数组，如 {loc: [{type: 'Point', coordinates: [0,0]}, {type: 'LineString', coordinates: [[1,1],[2,2]]}]}，并在数组字段上创建 2dsphere 索引。
   ```
2. ```
   删除 GeometryCollection 文档，或在聚合中使用 $match 阶段在 $geoNear 阶段之前跳过它。
   ```

## 无效尝试

- **** — Creating a sparse index does not bypass the type check; it only skips documents without the field. (95% 失败率)
- **** — Using $geoNear with an aggregation pipeline fails with the same error because the index is still applied. (90% 失败率)
