$minDistance
Definição
$minDistance
Filters the results of a geospatial
$near
or$nearSphere
query to those documents that are at least the specified distance from the center point.If
$near
or$nearSphere
query specifies the center point as a GeoJSON point, specify the distance as a non-negative number in meters.If
$nearSphere
query specifies the center point as legacy coordinate pair, specify the distance as a non-negative number in radians.$near
can only use the 2dsphere index if the query specifies the center point as a GeoJSON point.
Exemplos
Use with $near
Importante
Se especificar coordenadas de latitude e longitude, liste a longitude primeiro e então a latitude.
Os valores de longitude válidos estão entre
-180
e180
, ambos inclusos.Os valores de latitude válidos estão entre
-90
e90
, ambos inclusos.
Considere uma collection places
que tenha um índice 2dsphere
.
O exemplo a seguir retorna documentos que estão a pelo menos 1000
metros e no máximo a 5000
metros do ponto GeoJSON especificado, classificados do mais próximo para o mais distante:
db.places.find( { location: { $near : { $geometry: { type: "Point", coordinates: [ -73.9667, 40.78 ] }, $minDistance: 1000, $maxDistance: 5000 } } } )
Use with $nearSphere
Consider a collection places
that contains documents with a
location
field and has a 2dsphere index.
Then, the following example returns whose location
is at least
1000
meters from and at most 5000
meters from the specified
point, ordered from nearest to farthest:
db.places.find( { location: { $nearSphere: { $geometry: { type : "Point", coordinates : [ -73.9667, 40.78 ] }, $minDistance: 1000, $maxDistance: 5000 } } } )
For an example that specifies the center point as legacy coordinate
pair, see $nearSphere