mongodb data_error ai_generated true

MongoServerError: Can't extract geo keys: Point must be an array or object with 'type' and 'coordinates' properties

ID: mongodb/geo-index-2dsphere-invalid-point

Also available as: JSON · Markdown · 中文
90%Fix Rate
88%Confidence
1Evidence
2023-06-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
MongoDB 5.0 active
MongoDB 6.0 active
MongoDB 7.0 active

Root Cause

Documents in a collection with a 2dsphere index contain GeoJSON objects that are malformed, missing required fields (type/coordinates), or have invalid coordinate values (e.g., latitude outside ±90 or longitude outside ±180).

generic

中文

在具有 2dsphere 索引的集合中,文档包含格式错误的 GeoJSON 对象,缺少必需字段(type/coordinates),或坐标值无效(例如纬度超出 ±90 或经度超出 ±180)。

Official Documentation

https://www.mongodb.com/docs/manual/geospatial-queries/

Workarounds

  1. 90% success Find invalid documents using a script like: `db.collection.find({ "loc": { $not: { $type: "object" } } })` or check for missing fields: `db.collection.find({ "loc.type": { $exists: false } })`. Then update or remove them.
    Find invalid documents using a script like: `db.collection.find({ "loc": { $not: { $type: "object" } } })` or check for missing fields: `db.collection.find({ "loc.type": { $exists: false } })`. Then update or remove them.
  2. 85% success Use `db.collection.aggregate([{ $addFields: { loc: { $cond: { if: { $eq: [{ $type: "$loc" }, "array"] }, then: { type: "Point", coordinates: "$loc" }, else: "$loc" } } } }, { $out: "collection" }])` to convert legacy arrays to GeoJSON.
    Use `db.collection.aggregate([{ $addFields: { loc: { $cond: { if: { $eq: [{ $type: "$loc" }, "array"] }, then: { type: "Point", coordinates: "$loc" }, else: "$loc" } } } }, { $out: "collection" }])` to convert legacy arrays to GeoJSON.
  3. 80% success If coordinates are out of range, clamp them: `db.collection.updateMany({}, [{ $set: { "loc.coordinates.0": { $min: [ { $max: [ "$loc.coordinates.0", -180 ] }, 180 ] } } }])` (repeat for latitude).
    If coordinates are out of range, clamp them: `db.collection.updateMany({}, [{ $set: { "loc.coordinates.0": { $min: [ { $max: [ "$loc.coordinates.0", -180 ] }, 180 ] } } }])` (repeat for latitude).

中文步骤

  1. 使用脚本查找无效文档:`db.collection.find({ "loc": { $not: { $type: "object" } } })` 或检查缺失字段:`db.collection.find({ "loc.type": { $exists: false } })`。然后更新或删除它们。
  2. 使用 `db.collection.aggregate([{ $addFields: { loc: { $cond: { if: { $eq: [{ $type: "$loc" }, "array"] }, then: { type: "Point", coordinates: "$loc" }, else: "$loc" } } } }, { $out: "collection" }])` 将旧式数组转换为 GeoJSON。
  3. 如果坐标超出范围,进行钳制:`db.collection.updateMany({}, [{ $set: { "loc.coordinates.0": { $min: [ { $max: [ "$loc.coordinates.0", -180 ] }, 180 ] } } }])`(对纬度重复)。

Dead Ends

Common approaches that don't work:

  1. 95% fail

    The invalid documents remain in the collection and will cause the same error when the index is rebuilt or when querying.

  2. 80% fail

    2dsphere indexes only support GeoJSON format; legacy pairs require a 2d index, not 2dsphere.

  3. 60% fail

    Human error can introduce additional malformed data; a scripted approach is safer.