mongodb data_error ai_generated true

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

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

其他格式: JSON · Markdown 中文 · English
92%修复率
89%置信度
1证据数
2023-11-05首次发现

版本兼容性

版本状态引入弃用备注
MongoDB 4.4 active
MongoDB 5.0 active
MongoDB 6.0 active
MongoDB 7.0 active

根因分析

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

English

A 2dsphere index cannot index a GeoJSON GeometryCollection directly; it must be decomposed into individual geometries.

generic

官方文档

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

解决方案

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

无效尝试

常见但无效的做法:

  1. 95% 失败

    Creating a sparse index does not bypass the type check; it only skips documents without the field.

  2. 90% 失败

    Using $geoNear with an aggregation pipeline fails with the same error because the index is still applied.