$minDistance
定义
$minDistance
筛选空间查询的结果
$near
或$nearSphere
查询将结果过滤为那些距离中心点至少指定距离的文档。如果
$near
或$nearSphere
查询将中心点指定为 GeoJSON 点,则指定距离为以米为单位的非负数。如果
$nearSphere
查询将中心点指定为 旧版坐标对,则指定距离为以弧度为单位的非负数。如果$near
查询将中心点指定为 GeoJSON 点,则只能使用 2dsphere 索引。
示例
与 $near
结合使用
重要
如果指定经纬度坐标,请先列出 经度,然后是 纬度。
有效的经度值在
-180
到180
之间,两者都包含。有效的纬度值在
-90
到90
之间,两者都包含。
考虑一个具有 2dsphere
索引的集合 places
。
以下示例返回至少 1000
米和最多 5000
米距离指定 GeoJSON 点的文档,按从近到远排序
db.places.find( { location: { $near : { $geometry: { type: "Point", coordinates: [ -73.9667, 40.78 ] }, $minDistance: 1000, $maxDistance: 5000 } } } )
与 $nearSphere
结合使用
考虑一个包含具有 location
字段且具有 2dsphere 索引的文档的集合 places
。
然后,以下示例返回距离指定点至少 1000
米和最多 5000
米的 location
,按从近到远排序
db.places.find( { location: { $nearSphere: { $geometry: { type : "Point", coordinates : [ -73.9667, 40.78 ] }, $minDistance: 1000, $maxDistance: 5000 } } } )
有关指定中心点为旧坐标对的示例,请参阅 $nearSphere