$geoIntersects
Selects documents whose location field intersects with a specified GeoJSON object, useful for finding locations that intersect with specific geographical areas.
Syntax
{ <location field>: { $geoIntersects: { $geometry: { type: <GeoJSON type>, coordinates: <coordinates> } } } }
Parameters
location field
stringrequiredThe field containing the GeoJSON object
type
stringrequiredThe GeoJSON object type (e.g., "Polygon", "MultiPolygon")
coordinates
objectrequiredThe coordinates defining the GeoJSON object
Examples
Find stores intersecting with polygon
Find stores that intersect with a specific polygon area
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:
Documents whose locations intersect with the defined polygon
Intersection query with projection
Find intersecting locations and return only name and location fields
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]]] } } } }, { name: 1, location: 1 })
Output:
Limited projection of documents intersecting with polygon boundary