# db.command.geoNear

To set up a geographical location index for the queried field

Finds out the records in which the value of a field is close to the given point, and displays the records sorted in ascending order of a distance between the value and the given point.

Method signature:

function geoNear(IOptions): Command

interface IOptions {
  geometry: Point // Geographical location of the point
  maxDistance?: number // (Optional) Maximum distance (in meters).
  minDistance?: number // (Optional) Minimum distance (in meters).
}

Example: Find out the records in which the distance between the value and the given location is within 1 km to 5 km

const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
const _ = db.command
exports.main = async (event, context) => {
  return await db.collection('restaurants').where({
    location: _.geoNear({
      geometry: db.Geo.Point(113.323809, 23.097732),
      minDistance: 1000,
      maxDistance: 5000,
    })
  })
}
点击咨询小助手