nearSphere

abstract fun Field<T, Geo.Point>.nearSphere(target: Geo.Point, minDistance: Double? = null, maxDistance: Double? = null)(source)
open fun KProperty1<T, Geo.Point>.nearSphere(target: Geo.Point, minDistance: Double? = null, maxDistance: Double? = null)(source)

Matches documents where a Geo.Point is near the target, using spherical geometry.

Unlike near, uses spherical geometry for distance calculations, so results are accurate across large areas.

Documents are returned sorted, from the closest to the furthest. For the best performance, avoid specifying an additional sort.

Example

Find all bear sightings with 1km of Périgueux:

class BearSightings(
val _id: ObjectId,
val location: Geo.Point,
)

sightings.find {
BearSightings::location.nearSphere(
target = Geo.Point(Longitude(0.7269), Latitude(45.1828)),
maxDistance = 1000.0,
)
}.toList()

Indexing

This operator requires a 2dsphere index.

This operator cannot be combined with other operators requiring special indexes, like $text.

This operator is not permitted inside an aggregation pipeline. Instead, use the $geoNear stage.

External resources

Parameters

target

The point to search near.

minDistance

If specified, only matches documents that are further away from the target than this distance, in meters.

maxDistance

If specified, only matches documents that are closer to the target than this distance, in meters.