支持端:小程序 2.7.4, 云函数 0.8.1, Web
聚合阶段。将记录按照离给定点从近到远输出。
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
near | GeoPoint | 是 | GeoJSON Point,用于判断距离的点 | |
spherical | true | 是 | 必填,值为 true | |
limit | number | 否 | 限制返回记录数 | |
maxDistance | number | 否 | 距离最大值 | |
minDistance | number | 否 | 距离最小值 | |
query | Object | 否 | 要求记录必须同时满足该条件(语法同 where) | |
distanceMultiplier | number | 否 | 返回时在距离上乘以该数字 | |
distanceField | string | 是 | 存放距离的输出字段名,可以用点表示法表示一个嵌套字段 | |
includeLocs | string | 否 | 列出要用于距离计算的字段,如果记录中有多个字段都是地理位置时有用 | |
key | string | 否 | 选择要用的地理位置索引。如果集合由多个地理位置索引,则必须指定一个,指定的方式是指定对应的字段 |
假设集合 attractions 有如下记录:
{
"_id": "geoNear.0",
"city": "Guangzhou",
"docType": "geoNear",
"location": {
"type": "Point",
"coordinates": [
113.30593,
23.1361155
]
},
"name": "Canton Tower"
},
{
"_id": "geoNear.1",
"city": "Guangzhou",
"docType": "geoNear",
"location": {
"type": "Point",
"coordinates": [
113.306789,
23.1564721
]
},
"name": "Baiyun Mountain"
},
{
"_id": "geoNear.2",
"city": "Beijing",
"docType": "geoNear",
"location": {
"type": "Point",
"coordinates": [
116.3949659,
39.9163447
]
},
"name": "The Palace Museum"
},
{
"_id": "geoNear.3",
"city": "Beijing",
"docType": "geoNear",
"location": {
"type": "Point",
"coordinates": [
116.2328567,
40.242373
]
},
"name": "Great Wall"
}
const $ = db.command.aggregate
db.collection("attractions").aggregate()
.geoNear({
distanceField: "distance", // 输出的每个记录中 distance 即是与给定点的距离
spherical: true,
near: db.Geo.Point(113.3089506, 23.0968251),
query: {
docType: "geoNear",
},
key: "location", // 若只有 location 一个地理位置索引的字段,则不需填
includeLocs: "location", // 若只有 location 一个是地理位置,则不需填
})
.end()
返回结果如下:
{
"_id": "geoNear.0",
"location": {
"type": "Point",
"coordinates": [
113.30593,
23.1361155
]
},
"docType": "geoNear",
"name": "Canton Tower",
"city": "Guangzhou",
"distance": 4384.68131486958
},
{
"_id": "geoNear.1",
"city": "Guangzhou",
"location": {
"type": "Point",
"coordinates": [
113.306789,
23.1564721
]
},
"docType": "geoNear",
"name": "Baiyun Mountain",
"distance": 6643.521654040738
},
{
"_id": "geoNear.2",
"docType": "geoNear",
"name": "The Palace Museum",
"city": "Beijing",
"location": {
"coordinates": [
116.3949659,
39.9163447
],
"type": "Point"
},
"distance": 1894750.4414538583
},
{
"_id": "geoNear.3",
"docType": "geoNear",
"name": "Great Wall",
"city": "Beijing",
"location": {
"type": "Point",
"coordinates": [
116.2328567,
40.242373
]
},
"distance": 1928300.3308822548
}
AggregateCommand.arrayElemAt(value:Expression[]): Object支持端:小程序 2.7.4,云函数 0.8.1,Web聚合操作符。返回在指定数组...
本节例子中代码使用的单文件组件语法#computed使用 getter 函数,并为从 getter 返回的值返回一个不变的响应式 ref 对象。const ...
该页面假设你已经阅读过了组件基础。如果你还对组件不太了解,推荐你先阅读它。尽管存在 prop 和事件,但有时你可能仍然需要直接...
#介绍当构建可靠的应用时,测试在个人或团队构建新特性、重构代码、修复 bug 等工作中扮演了关键的角色。尽管测试的流派有很多,...
概览(Tour)基本概念(Basic Concepts)Sublime Text的界面如下:标签(Tab):无需介绍。编辑区(Editing Area):无需介绍。...