SDK數(shù)據(jù)庫 Aggregate·從近到遠(yuǎn)輸出

2022-05-12 16:46 更新

Aggregate.geoNear(options: Object): Aggregate

支持端:小程序 2.7.4, 云函數(shù) 0.8.1, Web

聚合階段。將記錄按照離給定點(diǎn)從近到遠(yuǎn)輸出。

參數(shù)

options: Object

屬性類型默認(rèn)值必填說明
nearGeoPointGeoJSON Point,用于判斷距離的點(diǎn)
sphericaltrue必填,值為 true
limitnumber限制返回記錄數(shù)
maxDistancenumber距離最大值
minDistancenumber距離最小值
queryObject要求記錄必須同時(shí)滿足該條件(語法同 where)
distanceMultipliernumber返回時(shí)在距離上乘以該數(shù)字
distanceFieldstring存放距離的輸出字段名,可以用點(diǎn)表示法表示一個(gè)嵌套字段
includeLocsstring列出要用于距離計(jì)算的字段,如果記錄中有多個(gè)字段都是地理位置時(shí)有用
keystring選擇要用的地理位置索引。如果集合由多個(gè)地理位置索引,則必須指定一個(gè),指定的方式是指定對應(yīng)的字段

返回值

Aggregate

API 說明

  • 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
}


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號