near

abstract fun near(target: Geo.Point, minDistance: Double? = null, maxDistance: Double? = null)(source)

Matches documents where a Geo.Point is near the target.

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

For large areas, in which the curvature of the Earth becomes significant, consider using nearSphere instead.

This operator should only be called on fields of type Geo.Point.

Example

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

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

sightings.find {
BearSightings::location {
near(
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.

See also

Convenience method with better type-safety.