# 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`
- **Domain:** mongodb
- **Category:** data_error
- **Verification:** ai_generated
- **Fix Rate:** 90%

## 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).

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| MongoDB 5.0 | active | — | — |
| MongoDB 6.0 | active | — | — |
| MongoDB 7.0 | active | — | — |

## Workarounds

1. **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.** (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.
   ```
2. **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.** (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.
   ```
3. **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).** (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).
   ```

## Dead Ends

- **** — The invalid documents remain in the collection and will cause the same error when the index is rebuilt or when querying. (95% fail)
- **** — 2dsphere indexes only support GeoJSON format; legacy pairs require a 2d index, not 2dsphere. (80% fail)
- **** — Human error can introduce additional malformed data; a scripted approach is safer. (60% fail)
