$minDistance

Specifies the minimum distance (in meters) that must exist between two points in a geospatial query, useful for finding locations outside a certain radius.

Syntax

{ <location field>: { $near: { $geometry: { type: "Point", coordinates: [<longitude>, <latitude>] }, $minDistance: <distance_in_meters> } } }

Parameters

location fieldstringrequired

The field containing the geospatial data

coordinatesobjectrequired

Array of [longitude, latitude] specifying the center point

distance_in_metersnumberrequired

Minimum distance in meters from the center point

Examples

Find stores beyond minimum distance

Find stores at least 500 kilometers away from a reference point

Query:

db.stores.find({ location: { $near: { $geometry: { type: "Point", coordinates: [69.7296, 70.1272] }, $minDistance: 500000 } } })

Output:

Stores located at least 500000 meters (500km) from the specified coordinates

Minimum distance with field selection

Find distant stores and return only specific fields

Query:

db.stores.find({ location: { $near: { $geometry: { type: "Point", coordinates: [69.7296, 70.1272] }, $minDistance: 100000 } } }, { name: 1, location: 1 })

Output:

Name and location of stores at least 100km from the center point

Related