本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
$maxDistance
Amazon DocumentDB 中的$maxDistance运算符用于指定距离 GeoJSON 点的最大距离(以米为单位),文档必须在该距离之内才能包含在查询结果中。此运算符与运$nearSphere算符配合使用以执行地理空间查询。
参数
-
$maxDistance:距离参考点的最大距离(以米为单位),文档必须在该距离之内才能包含在查询结果中。
示例(MongoDB 外壳)
以下示例演示如何使用 Amazon DocumentDB 中的$maxDistance运算符来查找距离波士顿 100 千米范围内的所有州首府。
创建示例文档
db.capitals.insert([ { state: "Massachusetts", city: "Boston", location: { type: "Point", coordinates: [-71.0589, 42.3601] } }, { state: "Rhode Island", city: "Providence", location: { type: "Point", coordinates: [-71.4128, 41.8239] } }, { state: "New Hampshire", city: "Concord", location: { type: "Point", coordinates: [-71.5383, 43.2067] } }, { state: "Vermont", city: "Montpelier", location: { type: "Point", coordinates: [-72.5751, 44.2604] } } ]);
查询示例
db.capitals.find( { location: { $nearSphere: { $geometry: { type: "Point", coordinates: [-71.0589, 42.3601] }, $maxDistance: 100000 } } }, { state: 1, city: 1, _id: 0 } );
输出
[
{ "state": "Rhode Island", "city": "Providence" },
{ "state": "New Hampshire", "city": "Concord" }
]
代码示例
要查看使用该$maxDistance命令的代码示例,请选择要使用的语言的选项卡: