# MongoServerError: 找不到索引：{ "geometry" : "2dsphere" } 在命名空间 test.places 中

- **ID:** `mongodb/geo-2dsphere-index-missing`
- **领域:** mongodb
- **类别:** config_error
- **错误码:** `17067`
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

地理空间查询或操作需要在 geometry 字段上存在 2dsphere 索引，但该索引不存在或定义不正确。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| mongodb-3.6 | active | — | — |
| mongodb-4.0 | active | — | — |
| mongodb-4.2 | active | — | — |
| mongodb-4.4 | active | — | — |
| mongodb-5.0 | active | — | — |
| mongodb-6.0 | active | — | — |
| mongodb-7.0 | active | — | — |

## 解决方案

1. ```
   Create a 2dsphere index on the geometry field: `db.places.createIndex({geometry:'2dsphere'})`. Ensure the field contains valid GeoJSON objects.
   ```
2. ```
   If the field name is wrong, update the query to use an existing 2dsphere index. Check existing indexes with `db.places.getIndexes()` and adjust the query field accordingly.
   ```
3. ```
   If the data is legacy coordinate pairs, convert them to GeoJSON format using an aggregation pipeline with $addFields and $project, then create the 2dsphere index. Example: `db.places.aggregate([{$addFields:{geometry:{type:'Point',coordinates:['$lon','$lat']}}},{$out:'places'}]); db.places.createIndex({geometry:'2dsphere'})`
   ```

## 无效尝试

- **** — 2d index only supports legacy coordinate pairs, not GeoJSON objects like 'Point' or 'Polygon'. (30% 失败率)
- **** — This does not create the required index; the query still fails. (10% 失败率)
- **** — The index must be on the exact field used in the query's $near or $geoWithin. (25% 失败率)
