KeplerGrid guide

Find MongoDB points inside a GeoJSON polygon with $geoWithin

Query store coordinates within a service area. Use longitude first, close the ring, and check the returned points in KeplerGrid.

Load the practice collection

Run once in mongosh against a development instance with an empty pickupPoints practice collection.

const lab = db.getSiblingDB("keplergrid_practice")
lab.pickupPoints.insertMany([
  { _id: "Library", location: { type: "Point", coordinates: [-3.71, 40.42] } },
  { _id: "Station", location: { type: "Point", coordinates: [-3.69, 40.41] } },
  { _id: "Depot", location: { type: "Point", coordinates: [-3.75, 40.45] } }
])
lab.pickupPoints.createIndex({ location: "2dsphere" })

In KeplerGrid, open keplergrid_practice.pickupPoints with a Read only connection.

Paste the polygon into Documents

Use a small rectangular polygon in the Documents filter. GeoJSON stores longitude before latitude. The last point repeats the first to close the ring.

{
  "location": { "$geoWithin": { "$geometry": {
    "type": "Polygon",
    "coordinates": [[
      [-3.72, 40.40], [-3.68, 40.40], [-3.68, 40.43],
      [-3.72, 40.43], [-3.72, 40.40]
    ]]
  } } }
}

The result includes Library and Station. Depot is outside. The locations are synthetic practice data.

KeplerGrid Documents shows exactly two matches, Library and Station, for the practice polygon
The two points inside the practice polygon appear in the Documents results.

Check the shape if a point is unexpectedly missing

Confirm that the field contains GeoJSON Points and that the polygon uses the same coordinate order. Start with points clearly inside or outside to verify the query before investigating points on an edge.

The 2dsphere index can support this filter. $geoWithin checks containment and returns no guaranteed distance order.

See how to inspect a pipeline’s execution plan or revisit the related KeplerGrid walkthrough.