新聞中心
這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
創(chuàng)新互聯(lián)小程序教程:SDK數(shù)據(jù)庫(kù)Aggregate·從近到遠(yuǎn)輸出
Aggregate.geoNear(options: Object): Aggregate
支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web
創(chuàng)新互聯(lián)堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計(jì)制作、成都網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶(hù)于互聯(lián)網(wǎng)時(shí)代的蘆淞網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
聚合階段。將記錄按照離給定點(diǎn)從近到遠(yuǎn)輸出。
參數(shù)
options: Object
| 屬性 | 類(lèi)型 | 默認(rèn)值 | 必填 | 說(shuō)明 |
|---|---|---|---|---|
| near | GeoPoint | 是 | GeoJSON Point,用于判斷距離的點(diǎn) | |
| spherical | true | 是 | 必填,值為 true | |
| limit | number | 否 | 限制返回記錄數(shù) | |
| maxDistance | number | 否 | 距離最大值 | |
| minDistance | number | 否 | 距離最小值 | |
| query | Object | 否 | 要求記錄必須同時(shí)滿足該條件(語(yǔ)法同 where) | |
| distanceMultiplier | number | 否 | 返回時(shí)在距離上乘以該數(shù)字 | |
| distanceField | string | 是 | 存放距離的輸出字段名,可以用點(diǎn)表示法表示一個(gè)嵌套字段 | |
| includeLocs | string | 否 | 列出要用于距離計(jì)算的字段,如果記錄中有多個(gè)字段都是地理位置時(shí)有用 | |
| key | string | 否 | 選擇要用的地理位置索引。如果集合由多個(gè)地理位置索引,則必須指定一個(gè),指定的方式是指定對(duì)應(yīng)的字段 |
返回值
Aggregate
API 說(shuō)明
- geoNear 必須為第一個(gè)聚合階段
- 必須有地理位置索引。如果有多個(gè),必須用 key 參數(shù)指定要使用的索引。
示例
假設(shè)集合 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', // 輸出的每個(gè)記錄中 distance 即是與給定點(diǎn)的距離
spherical: true,
near: db.Geo.Point(113.3089506, 23.0968251),
query: {
docType: 'geoNear',
},
key: 'location', // 若只有 location 一個(gè)地理位置索引的字段,則不需填
includeLocs: 'location', // 若只有 location 一個(gè)是地理位置,則不需填
})
.end()
返回結(jié)果如下:
{
"_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
} 當(dāng)前標(biāo)題:創(chuàng)新互聯(lián)小程序教程:SDK數(shù)據(jù)庫(kù)Aggregate·從近到遠(yuǎn)輸出
文章位置:http://www.5511xx.com/article/cogieeh.html


咨詢(xún)
建站咨詢(xún)

