$geometry

Specifies a GeoJSON geometry object for geospatial queries, used within other geospatial operators to define shapes and points for spatial calculations.

Syntax

{ $geometry: { type: <GeoJSON type>, coordinates: <coordinates> } }

Parameters

typestringrequired

GeoJSON object type (Point, Polygon, MultiPolygon, etc.)

coordinatesobjectrequired

Coordinates defining the GeoJSON object as an array

Examples

Find nearest stores to point geometry

Use geometry operator to define a point for nearest location search

Query:

db.stores.find({ location: { $near: { $geometry: { type: "Point", coordinates: [46.2917, -62.6354] } } } })

Output:

Stores ordered by distance from the specified point geometry

Find stores within polygon geometry

Use geometry operator to define polygon boundaries for location search

Query:

db.stores.find({ location: { $geoIntersects: { $geometry: { type: "Polygon", coordinates: [[[-80.0, -75.0], [-80.0, -70.0], [-55.0, -70.0], [-55.0, -75.0], [-80.0, -75.0]]] } } } })

Output:

Stores that intersect with the polygon geometry

Multi-polygon geometry query

Use geometry operator with MultiPolygon for complex area searches

Query:

db.stores.find({ location: { $geoWithin: { $geometry: { type: "MultiPolygon", coordinates: [[[120.0, -13.0], [120.0, -10.0], [125.0, -10.0], [125.0, -13.0], [120.0, -13.0]], [[44.0, -64.0], [44.0, -61.0], [48.0, -61.0], [48.0, -64.0], [44.0, -64.0]]] } } } })

Output:

Stores within any of the specified polygon areas

Related