Aggregate.geoNear(options: Object): Aggregate

支持端:小程序 2.7.4 起, 云函数 0.8.1

聚合阶段。将记录按照离给定点从近到远输出。

参数

options: Object

属性类型默认值必填说明
nearGeoPointGeoJSON Point,用于判断距离的点
sphericaltrue必填,值为 true
limitnumber限制返回记录数
maxDistancenumber距离最大值
minDistancenumber距离最小值
queryObject要求记录必须同时满足该条件(语法同 where)
distanceMultipliernumber返回时在距离上乘以该数字
distanceFieldstring存放距离的输出字段名,可以用点表示法表示一个嵌套字段
includeLocsstring列出要用于距离计算的字段,如果记录中有多个字段都是地理位置时有用
keystring选择要用的地理位置索引。如果集合由多个地理位置索引,则必须指定一个,指定的方式是指定对应的字段

返回值

Aggregate

API 说明

  • geoNear 必须为第一个聚合阶段
  • 必须有地理位置索引。如果有多个,必须用 key 参数指定要使用的索引。

示例

假设集合 attractions 有如下记录:

  1. {
  2. "_id": "geoNear.0",
  3. "city": "Guangzhou",
  4. "docType": "geoNear",
  5. "location": {
  6. "type": "Point",
  7. "coordinates": [
  8. 113.30593,
  9. 23.1361155
  10. ]
  11. },
  12. "name": "Canton Tower"
  13. },
  14. {
  15. "_id": "geoNear.1",
  16. "city": "Guangzhou",
  17. "docType": "geoNear",
  18. "location": {
  19. "type": "Point",
  20. "coordinates": [
  21. 113.306789,
  22. 23.1564721
  23. ]
  24. },
  25. "name": "Baiyun Mountain"
  26. },
  27. {
  28. "_id": "geoNear.2",
  29. "city": "Beijing",
  30. "docType": "geoNear",
  31. "location": {
  32. "type": "Point",
  33. "coordinates": [
  34. 116.3949659,
  35. 39.9163447
  36. ]
  37. },
  38. "name": "The Palace Museum"
  39. },
  40. {
  41. "_id": "geoNear.3",
  42. "city": "Beijing",
  43. "docType": "geoNear",
  44. "location": {
  45. "type": "Point",
  46. "coordinates": [
  47. 116.2328567,
  48. 40.242373
  49. ]
  50. },
  51. "name": "Great Wall"
  52. }
  1. const $ = db.command.aggregate
  2. db.collection('attractions').aggregate()
  3. .geoNear({
  4. distanceField: 'distance', // 输出的每个记录中 distance 即是与给定点的距离
  5. spherical: true,
  6. near: db.Geo.Point(113.3089506, 23.0968251),
  7. query: {
  8. docType: 'geoNear',
  9. },
  10. key: 'location', // 若只有 location 一个地理位置索引的字段,则不需填
  11. includeLocs: 'location', // 若只有 location 一个是地理位置,则不需填
  12. })
  13. .end()

返回结果如下:

  1. {
  2. "_id": "geoNear.0",
  3. "location": {
  4. "type": "Point",
  5. "coordinates": [
  6. 113.30593,
  7. 23.1361155
  8. ]
  9. },
  10. "docType": "geoNear",
  11. "name": "Canton Tower",
  12. "city": "Guangzhou",
  13. "distance": 4384.68131486958
  14. },
  15. {
  16. "_id": "geoNear.1",
  17. "city": "Guangzhou",
  18. "location": {
  19. "type": "Point",
  20. "coordinates": [
  21. 113.306789,
  22. 23.1564721
  23. ]
  24. },
  25. "docType": "geoNear",
  26. "name": "Baiyun Mountain",
  27. "distance": 6643.521654040738
  28. },
  29. {
  30. "_id": "geoNear.2",
  31. "docType": "geoNear",
  32. "name": "The Palace Museum",
  33. "city": "Beijing",
  34. "location": {
  35. "coordinates": [
  36. 116.3949659,
  37. 39.9163447
  38. ],
  39. "type": "Point"
  40. },
  41. "distance": 1894750.4414538583
  42. },
  43. {
  44. "_id": "geoNear.3",
  45. "docType": "geoNear",
  46. "name": "Great Wall",
  47. "city": "Beijing",
  48. "location": {
  49. "type": "Point",
  50. "coordinates": [
  51. 116.2328567,
  52. 40.242373
  53. ]
  54. },
  55. "distance": 1928300.3308822548
  56. }