mongodb
data_error
ai_generated
true
MongoServerError: Can't extract geo keys: 2dsphere index requires 'Point', 'MultiPoint', 'LineString', 'MultiLineString', 'Polygon', or 'MultiPolygon' GeoJSON type, but found 'GeometryCollection'
ID: mongodb/geo-index-2dsphere-unsupported-type
92%Fix Rate
89%Confidence
1Evidence
2023-11-05First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| MongoDB 4.4 | active | — | — | — |
| MongoDB 5.0 | active | — | — | — |
| MongoDB 6.0 | active | — | — | — |
| MongoDB 7.0 | active | — | — | — |
Root Cause
A 2dsphere index cannot index a GeoJSON GeometryCollection directly; it must be decomposed into individual geometries.
generic中文
2dsphere 索引无法直接索引 GeoJSON GeometryCollection;必须将其分解为单独的几何对象。
Official Documentation
https://www.mongodb.com/docs/manual/core/2dsphere/Workarounds
-
90% success Convert GeometryCollection to an array of individual geometries in the document: update the field to an array like {loc: [{type: 'Point', coordinates: [0,0]}, {type: 'LineString', coordinates: [[1,1],[2,2]]}]} and create a 2dsphere index on the array field.
Convert GeometryCollection to an array of individual geometries in the document: update the field to an array like {loc: [{type: 'Point', coordinates: [0,0]}, {type: 'LineString', coordinates: [[1,1],[2,2]]}]} and create a 2dsphere index on the array field. -
85% success Remove the GeometryCollection document or skip it using a $match stage before the $geoNear stage in aggregation.
Remove the GeometryCollection document or skip it using a $match stage before the $geoNear stage in aggregation.
中文步骤
将 GeometryCollection 转换为文档中的单个几何对象数组:将字段更新为数组,如 {loc: [{type: 'Point', coordinates: [0,0]}, {type: 'LineString', coordinates: [[1,1],[2,2]]}]},并在数组字段上创建 2dsphere 索引。删除 GeometryCollection 文档,或在聚合中使用 $match 阶段在 $geoNear 阶段之前跳过它。
Dead Ends
Common approaches that don't work:
-
95% fail
Creating a sparse index does not bypass the type check; it only skips documents without the field.
-
90% fail
Using $geoNear with an aggregation pipeline fails with the same error because the index is still applied.