17067 mongodb config_error ai_generated true

MongoServerError: Can't find index: { "geometry" : "2dsphere" } for namespace test.places

ID: mongodb/geo-2dsphere-index-missing

Also available as: JSON · Markdown · 中文
90%Fix Rate
87%Confidence
1Evidence
2023-07-05First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
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

Root Cause

A geospatial query or operation requires a 2dsphere index on the geometry field, but the index does not exist or is incorrectly defined.

generic

中文

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

Official Documentation

https://www.mongodb.com/docs/manual/core/2dsphere/

Workarounds

  1. 95% success Create a 2dsphere index on the geometry field: `db.places.createIndex({geometry:'2dsphere'})`. Ensure the field contains valid GeoJSON objects.
    Create a 2dsphere index on the geometry field: `db.places.createIndex({geometry:'2dsphere'})`. Ensure the field contains valid GeoJSON objects.
  2. 85% success 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.
    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. 80% success 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'})`
    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'})`

中文步骤

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

Dead Ends

Common approaches that don't work:

  1. 30% fail

    2d index only supports legacy coordinate pairs, not GeoJSON objects like 'Point' or 'Polygon'.

  2. 10% fail

    This does not create the required index; the query still fails.

  3. 25% fail

    The index must be on the exact field used in the query's $near or $geoWithin.