Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

The SDK database Aggregate outputs from near to far


May 20, 2021 WeChat Mini Program Development Document


Table of contents


Aggregate.geoNear(options: Object): Aggregate

Support: Small program 2.7.4, cloud function 0.8.1, Web

The aggregation phase. The record is output from close to far from a given point.

Parameters

options: Object

Property Type The default Required Description
near GeoPoint Is GeoJSON Point, used to determine the point of distance
spherical true Is Required with a value of true
limit number Whether Limit the number of records returned
maxDistance number Whether The maximum distance
minDistance number Whether The minimum distance
query Object Whether Requires that the record meet the condition at the same time (syntax same as where)
distanceMultiplier number Whether Multiplies the number by the distance when you return
distanceField string Is The output field name of the storage distance, which can be represented by a nested field by point notation
includeLocs string Whether List the fields you want to use for distance calculations, which is useful if there are multiple fields in the record that are geographic locations
key string Whether Select the geographic location index to use. If the collection is indexed by more than one geographic location, you must specify one in a way that specifies the corresponding field

Returns a value

Aggregate

API description

  • geoNear must be the first aggregation stage
  • There must be a geo-location index. If you have more than one, you must specify the index to use with the key parameter.

Example

Suppose the collection attractions have the following records:

{
  "_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()

The results are as follows:

{
  "_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
}