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

SDK database Command query,geolocation operator


May 20, 2021 WeChat Mini Program Development Document


Table of contents


Command.geoNear(options: Object): Command

Support: small programs, cloud functions, web

Find records of field values near a given point in order from near to far.

Parameters

options: Object

Property Type The default Required Description
geometry GeoPoint Is Location Point
maxDistance number Whether Optional fill, maximum distance, in meters
minDistance number Whether Optional, minimum distance, in meters

Returns a value

Command

Index requirements

The query field needs to be indexed geographically

The sample code

Find records within 1 km to 5 km of a given location

const _ = db.command
db.collection('restaurants').where({
  location: _.geoNear({
    geometry: db.Geo.Point(113.323809, 23.097732),
    minDistance: 1000,
    maxDistance: 5000,
  })
}).get()

Command.geoWithin(options: Object): Command

Support: small programs, cloud functions, web

Find records with field values in the specified area, no sorting. The specified area must be Polygon or Polygon.

Parameters

options: Object

Property Type The default Required Description
geometry Object Is Geo-information structure, Polygon, MultiPolygon, or s centerSphere

Returns a value

Command

Index requirements

The query field needs to be indexed geographically

Example code 1: Given polygon

const _ = db.command
const { Point, LineString, Polygon } = db.Geo
db.collection('restaurants').where({
  location: _.geoWithin({
    geometry: Polygon([
      LineString([
        Point(0, 0),
        Point(3, 2),
        Point(2, 3),
        Point(0, 0)
      ])
    ]),
  })
})

Example code 2: Given a circle

You can build a circle with centerSphere instead of geometry.

CenterShpere starts with public library 2.8.3

The corresponding value of centerSphere is defined as: .

The radius needs to be measured in radian, e.g. a radius of 10km, divided by the distance by the earth radius of 6378.1km.

const _ = db.command
db.collection('restaurants').where({
  location: _.geoWithin({
    centerSphere: [
      [-88, 30],
      10 / 6378.1,
    ]
  })
})

Command.geoIntersects(options: Object): Command

Support: small programs, cloud functions, web

Find records where a given geographic graph intersects

Parameters

options: Object

Property Type The default Required Description
geometry Object Is Geographic information structure, Point

Returns a value

Command

Index requirements

The query field needs to be indexed geographically

Example code: Find a record that intersects a polygon

const _ = db.command
const { Point, LineString, Polygon } = db.Geo
db.collection('restaurants').where({
  location: _.geoIntersects({
    geometry: Polygon([
      LineString([
        Point(0, 0),
        Point(3, 2),
        Point(2, 3),
        Point(0, 0)
      ])
    ]),
  })
})