Geo-Spatial Indexes
ArangoDB features a Google S2 based geospatial indexsince version 3.4.0, which supersedes the previous geo index implementation.Indexing is supported for a subset of the GeoJSON geometry typesas well as simple latitude longitude pairs.
AQL’s geospatial functions and GeoJSON constructors are described inGeo functions.
Using a Geo-Spatial Index
The geospatial index supports containment and intersectionqueries for various geometric 2D shapes. You should be mainly using AQL queriesto perform these types of operations. The index can operate in two differentmodes, depending on if you want to use the GeoJSON data-format or not. The modesare mainly toggled by using the geoJson
field when creating the index.
This index assumes coordinates with the latitude between -90 and 90 degrees and thelongitude between -180 and 180 degrees. A geo index will ignore all documents which do not fulfill these requirements.
GeoJSON Mode
To create an index in GeoJSON mode execute:
collection.ensureIndex({ type: "geo", fields: [ "geometry" ], geoJson:true })
This creates the index on all documents and uses geometry as the attributedfield where the value is either a GeometryObjector a coordinatearray. The array must contain at least two numeric values with longitude (firstvalue) and the latitude (second value). This corresponds to the formatdescribed in RFC 7946Position
All documents, which do not have the attribute path or have a non-conformvalue in it, are excluded from the index.
A geo index is implicitly sparse, and there is no way to control its sparsity.In case that the index was successfully created, an object with the indexdetails, including the index-identifier, is returned.
Non-GeoJSON mode
This index mode exclusively supports indexing on coordinate arrays. Values thatcontain GeoJSON or other types of data will be ignored. In the non-GeoJSON modethe index can be created on one or two fields.
The following examples will work in the arangosh command shell.
To create a geo-spatial index on all documents using latitude andlongitude as separate attribute paths, two paths need to be specifiedin the fields array:
collection.ensureIndex({ type: "geo", fields: [ "latitude", "longitude" ] })
The first field is always defined to be the latitude and the second is thelongitude. The geoJson
flag is implicitly false in this mode.
Alternatively you can specify only one field:
collection.ensureIndex({ type: "geo", fields: [ "location" ], geoJson:false })
It creates a geospatial index on all documents using location as the path to thecoordinates. The value of the attribute has to be an array with at least twonumeric values. The array must contain the latitude (first value) and thelongitude (second value).
All documents, which do not have the attribute path(s) or have a non-conformingvalue in it, are excluded from the index.
A geo index is implicitly sparse, and there is no way to control its sparsity.In case that the index was successfully created, an object with the indexdetails, including the index-identifier, is returned.
In case that the index was successfully created, an object with the indexdetails, including the index-identifier, is returned.
Indexed GeoSpatial Queries
The geospatial index supports a variety of AQL queries, which can be built with the helpof the geo utility functions. There are three specificgeo functions that can be optimized, provided that they are used correctly:GEODISTANCE, GEO_CONTAINS, GEO_INTERSECTS
. Additionally, there is a built-in support to optimizethe older geo functions DISTANCE
, NEAR
and WITHIN
(the last two only if they areused in their 4 argument version, without _distanceName).
When in doubt whether your query is being properly optimized, check the AQL explainoutput to check for index usage.
Query for Results near Origin (NEAR type query)
A basic example of a query for results near an origin point:
FOR x IN geo_collection
FILTER GEO_DISTANCE([@lng, @lat], x.geometry) <= 100000
RETURN x._key
The first parameter can be a GeoJSON object or a coordinate array in [longitude, latitude]
ordering.The second parameter is the document field on which the index was created. The functionGEODISTANCE
always returns the distance in meters, so will receive resultsup until _100km.
Query for Sorted Results near Origin (NEAR type query)
A basic example of a query for the 1000 nearest results to an origin point (ascending sorting):
FOR x IN geo_collection
SORT GEO_DISTANCE([@lng, @lat], x.geometry) ASC
LIMIT 1000
RETURN x._key
The first parameter can be a GeoJSON object or a coordinate array in [longitude, latitude]
ordering.The second parameter is the documents field on which the index was created.
You may also get results farthest away (distance sorted in descending order):
FOR x IN geo_collection
SORT GEO_DISTANCE([@lng, @lat], x.geometry) DESC
LIMIT 1000
RETURN x._key
Query for Results within Distance
A query which returns documents at a distance of 1km or farther away,up to 100km from the origin. This will return the documents with a GeoJSONvalue that is located in the specified search annulus.
FOR x IN geo_collection
FILTER GEO_DISTANCE([@lng, @lat], x.geometry) <= 100000
FILTER GEO_DISTANCE([@lng, @lat], x.geometry) >= 1000
RETURN x
Query for Results contained in Polygon
A query which returns documents whose stored geometry is contained within aGeoJSON Polygon.
LET polygon = GEO_POLYGON([[[60,35],[50,5],[75,10],[70,35]]])
FOR x IN geo_collection
FILTER GEO_CONTAINS(polygon, x.geometry)
RETURN x
The first parameter of GEO_CONTAINS
must be a polygon. Other types are not valid. The second parameter must contain the document field on which the index was created.
Query for Results Intersecting a Polygon
A query which returns documents with an intersection of their stored geometry and aGeoJSON Polygon.
LET polygon = GEO_POLYGON([[[60,35],[50,5],[75,10],[70,35]]])
FOR x IN geo_collection
FILTER GEO_INTERSECTS(polygon, x.geometry)
RETURN x
The first parameter of GEO_INTERSECTS
must be a polygon. Other types are not valid. The second parameter must contain the document field on which the index was created.
GeoJSON
GeoJSON is a geospatial data format based on JSON. It defines several differenttypes of JSON objects and the way in which they can be combined to representdata about geographic shapes on the earth surface. GeoJSON uses a geographiccoordinate reference system, World Geodetic System 1984 (WGS 84), and units of decimaldegrees.
Internally ArangoDB maps all coordinates onto a unit sphere. Distances areprojected onto a sphere with the Earth’s Volumetric mean radius of 6371km. ArangoDB implements a useful subset of the GeoJSON format (RFC7946). We do not support Feature Objectsor the GeometryCollection type. Supported geometry object types are:
- Point
- MultiPoint
- LineString
- MultiLineString
- Polygon
Point
A GeoJSON Point is aposition comprised ofa longitude and a latitude:
{
"type": "Point",
"coordinates": [100.0, 0.0]
}
MultiPoint
A GeoJSON MultiPoint isan array of positions:
{
"type": "MultiPoint",
"coordinates": [
[100.0, 0.0],
[101.0, 1.0]
]
}
LineString
A GeoJSON LineString isan array of two or more positions:
{
"type": "LineString",
"coordinates": [
[100.0, 0.0],
[101.0, 1.0]
]
}
MultiLineString
A GeoJSON MultiLineString isan array of LineString coordinate arrays:
{
"type": "MultiLineString",
"coordinates": [
[
[100.0, 0.0],
[101.0, 1.0]
],
[
[102.0, 2.0],
[103.0, 3.0]
]
]
}
Polygon
A GeoJSON Polygon consistsof a series of closed LineString
objects (ring-like). These Linear Ring objectsconsist of four or more vertices with the first and last coordinate pairsbeing equal. Coordinates of a Polygon are an array of linear ring coordinatearrays. The first element in the array represents the exterior ring.Any subsequent elements represent interior rings (holes within the surface).
- A linear ring may not be empty, it needs at least three distinct coordinates
- Within the same linear ring consecutive coordinates may be the same, otherwise(except the first and last one) all coordinates need to be distinct
- A linear ring defines two regions on the sphere. ArangoDB will always interpretthe region of smaller area to be the interior of the ring. This introduces apractical limitation that no polygon may have an outer ring enclosing morethan half the Earth’s surface.No Holes:
{
"type": "Polygon",
"coordinates": [
[
[100.0, 0.0],
[101.0, 0.0],
[101.0, 1.0],
[100.0, 1.0],
[100.0, 0.0]
]
]
}
With Holes:
- The exterior ring should not self-intersect.
- The interior rings must be contained in the outer ring
- No two rings can cross each other, i.e. no ring may intersect boththe interior and exterior face of another ring
- Rings cannot share edges, they may however share vertices
- No ring may be empty
- Polygon rings should follow the right-hand rule for orientation(counterclockwise external rings, clockwise internal rings).
{
"type": "Polygon",
"coordinates": [
[
[100.0, 0.0],
[101.0, 0.0],
[101.0, 1.0],
[100.0, 1.0],
[100.0, 0.0]
],
[
[100.8, 0.8],
[100.8, 0.2],
[100.2, 0.2],
[100.2, 0.8],
[100.8, 0.8]
]
]
}
MultiPolygon
A GeoJSON MultiPolygon consistsof multiple polygons. The “coordinates” member is an array ofPolygon coordinate arrays.
- Polygons in the same MultiPolygon may not share edges, they may share coordinates
- Polygons and rings must not be empty
- A linear ring defines two regions on the sphere. ArangoDB will always interpretthe region of smaller area to be the interior of the ring. This introduces apractical limitation that no polygon may have an outer ring enclosing morethan half the Earth’s surface.
- Linear rings must follow the right-hand rule for orientation(counterclockwise external rings, clockwise internal rings).Example with two polygons, the second one with a hole:
{
"type": "MultiPolygon",
"coordinates": [
[
[
[102.0, 2.0],
[103.0, 2.0],
[103.0, 3.0],
[102.0, 3.0],
[102.0, 2.0]
]
],
[
[
[100.0, 0.0],
[101.0, 0.0],
[101.0, 1.0],
[100.0, 1.0],
[100.0, 0.0]
],
[
[100.2, 0.2],
[100.2, 0.8],
[100.8, 0.8],
[100.8, 0.2],
[100.2, 0.2]
]
]
]
}
Arangosh Examples
ensures that a geo index existscollection.ensureIndex({ type: "geo", fields: [ "location" ] })
Creates a geospatial index on all documents using location as the path to thecoordinates. The value of the attribute has to be an array with at least twonumeric values. The array must contain the latitude (first value) and thelongitude (second value).
All documents, which do not have the attribute path or have a non-conformingvalue in it, are excluded from the index.
A geo index is implicitly sparse, and there is no way to control its sparsity.
In case that the index was successfully created, an object with the indexdetails, including the index-identifier, is returned.
To create a geo index on an array attribute that contains longitude first, setthe geoJson attribute to true
. This corresponds to the format described inRFC 7946 Position
collection.ensureIndex({ type: "geo", fields: [ "location" ], geoJson: true })
To create a geo-spatial index on all documents using latitude and _longitude_as separate attribute paths, two paths need to be specified in the _fields_array:
collection.ensureIndex({ type: "geo", fields: [ "latitude", "longitude" ] })
In case that the index was successfully created, an object with the indexdetails, including the index-identifier, is returned.
Examples
Create a geo index for an array attribute:
- arangosh> db.geo.ensureIndex({ type: "geo", fields: [ "loc" ] });
- arangosh> for (i = -90; i <= 90; i += 10) {
- ........> for (j = -180; j <= 180; j += 10) {
- ........> db.geo.save({ name : "Name/" + i + "/" + j, loc: [ i, j ] });
- ........> }
- ........> }
- arangosh> db.geo.count();
- arangosh> db.geo.near(0, 0).limit(3).toArray();
- arangosh> db.geo.near(0, 0).count();
Show execution results
- {
- "bestIndexedLevel" : 17,
- "fields" : [
- "loc"
- ],
- "geoJson" : false,
- "id" : "geo/79441",
- "isNewlyCreated" : true,
- "maxNumCoverCells" : 8,
- "sparse" : true,
- "type" : "geo",
- "unique" : false,
- "worstIndexedLevel" : 4,
- "code" : 201
- }
- 703
- [
- {
- "_key" : "80145",
- "_id" : "geo/80145",
- "_rev" : "_ZP4PW7u---",
- "name" : "Name/0/0",
- "loc" : [
- 0,
- 0
- ]
- },
- {
- "_key" : "80219",
- "_id" : "geo/80219",
- "_rev" : "_ZP4PW8m---",
- "name" : "Name/10/0",
- "loc" : [
- 10,
- 0
- ]
- },
- {
- "_key" : "80147",
- "_id" : "geo/80147",
- "_rev" : "_ZP4PW7u--A",
- "name" : "Name/0/10",
- "loc" : [
- 0,
- 10
- ]
- }
- ]
- null
Hide execution results
Create a geo index for a hash array attribute:
- arangosh> db.geo2.ensureIndex({ type: "geo", fields: [ "location.latitude", "location.longitude" ] });
- arangosh> for (i = -90; i <= 90; i += 10) {
- ........> for (j = -180; j <= 180; j += 10) {
- ........> db.geo2.save({ name : "Name/" + i + "/" + j, location: { latitude : i, longitude : j } });
- ........> }
- ........> }
- arangosh> db.geo2.near(0, 0).limit(3).toArray();
Show execution results
- {
- "bestIndexedLevel" : 17,
- "fields" : [
- "location.latitude",
- "location.longitude"
- ],
- "geoJson" : false,
- "id" : "geo2/80866",
- "isNewlyCreated" : true,
- "maxNumCoverCells" : 8,
- "sparse" : true,
- "type" : "geo",
- "unique" : false,
- "worstIndexedLevel" : 4,
- "code" : 201
- }
- [
- {
- "_key" : "81570",
- "_id" : "geo2/81570",
- "_rev" : "_ZP4PXO6---",
- "name" : "Name/0/0",
- "location" : {
- "latitude" : 0,
- "longitude" : 0
- }
- },
- {
- "_key" : "81644",
- "_id" : "geo2/81644",
- "_rev" : "_ZP4PXPy--A",
- "name" : "Name/10/0",
- "location" : {
- "latitude" : 10,
- "longitude" : 0
- }
- },
- {
- "_key" : "81572",
- "_id" : "geo2/81572",
- "_rev" : "_ZP4PXO6--A",
- "name" : "Name/0/10",
- "location" : {
- "latitude" : 0,
- "longitude" : 10
- }
- }
- ]
Hide execution results
Use GeoIndex with AQL SORT statement:
- arangosh> db.geoSort.ensureIndex({ type: "geo", fields: [ "latitude", "longitude" ] });
- arangosh> for (i = -90; i <= 90; i += 10) {
- ........> for (j = -180; j <= 180; j += 10) {
- ........> db.geoSort.save({ name : "Name/" + i + "/" + j, latitude : i, longitude : j });
- ........> }
- ........> }
- arangosh> var query = "FOR doc in geoSort SORT DISTANCE(doc.latitude, doc.longitude, 0, 0) LIMIT 5 RETURN doc"
- arangosh> db._explain(query, {}, {colors: false});
- arangosh> db._query(query);
Show execution results
- {
- "bestIndexedLevel" : 17,
- "fields" : [
- "latitude",
- "longitude"
- ],
- "geoJson" : false,
- "id" : "geoSort/85133",
- "isNewlyCreated" : true,
- "maxNumCoverCells" : 8,
- "sparse" : true,
- "type" : "geo",
- "unique" : false,
- "worstIndexedLevel" : 4,
- "code" : 201
- }
- Query String (86 chars, cacheable: true):
- FOR doc in geoSort SORT DISTANCE(doc.latitude, doc.longitude, 0, 0) LIMIT 5 RETURN doc
- Execution plan:
- Id NodeType Est. Comment
- 1 SingletonNode 1 * ROOT
- 7 IndexNode 703 - FOR doc IN geoSort /* geo index scan */
- 5 LimitNode 5 - LIMIT 0, 5
- 6 ReturnNode 5 - RETURN doc
- Indexes used:
- By Type Collection Unique Sparse Selectivity Fields Ranges
- 7 geo geoSort false true n/a [ `latitude`, `longitude` ] (GEO_DISTANCE([ 0, 0 ], [ doc.`longitude`, doc.`latitude` ]) < "unlimited")
- Optimization rules applied:
- Id RuleName
- 1 geo-index-optimizer
- 2 remove-unnecessary-calculations-2
- [
- {
- "_key" : "85837",
- "_id" : "geoSort/85837",
- "_rev" : "_ZP4PYKC---",
- "name" : "Name/0/0",
- "latitude" : 0,
- "longitude" : 0
- },
- {
- "_key" : "85911",
- "_id" : "geoSort/85911",
- "_rev" : "_ZP4PYK6--C",
- "name" : "Name/10/0",
- "latitude" : 10,
- "longitude" : 0
- },
- {
- "_key" : "85839",
- "_id" : "geoSort/85839",
- "_rev" : "_ZP4PYKC--A",
- "name" : "Name/0/10",
- "latitude" : 0,
- "longitude" : 10
- },
- {
- "_key" : "85763",
- "_id" : "geoSort/85763",
- "_rev" : "_ZP4PYJG--C",
- "name" : "Name/-10/0",
- "latitude" : -10,
- "longitude" : 0
- },
- {
- "_key" : "85835",
- "_id" : "geoSort/85835",
- "_rev" : "_ZP4PYK---C",
- "name" : "Name/0/-10",
- "latitude" : 0,
- "longitude" : -10
- }
- ]
- [object ArangoQueryCursor, count: 5, cached: false, hasMore: false]
Hide execution results
Use GeoIndex with AQL FILTER statement:
- arangosh> db.geoFilter.ensureIndex({ type: "geo", fields: [ "latitude", "longitude" ] });
- arangosh> for (i = -90; i <= 90; i += 10) {
- ........> for (j = -180; j <= 180; j += 10) {
- ........> db.geoFilter.save({ name : "Name/" + i + "/" + j, latitude : i, longitude : j });
- ........> }
- ........> }
- arangosh> var query = "FOR doc in geoFilter FILTER DISTANCE(doc.latitude, doc.longitude, 0, 0) < 2000 RETURN doc"
- arangosh> db._explain(query, {}, {colors: false});
- arangosh> db._query(query);
Show execution results
- {
- "bestIndexedLevel" : 17,
- "fields" : [
- "latitude",
- "longitude"
- ],
- "geoJson" : false,
- "id" : "geoFilter/82286",
- "isNewlyCreated" : true,
- "maxNumCoverCells" : 8,
- "sparse" : true,
- "type" : "geo",
- "unique" : false,
- "worstIndexedLevel" : 4,
- "code" : 201
- }
- Query String (89 chars, cacheable: true):
- FOR doc in geoFilter FILTER DISTANCE(doc.latitude, doc.longitude, 0, 0) < 2000 RETURN doc
- Execution plan:
- Id NodeType Est. Comment
- 1 SingletonNode 1 * ROOT
- 6 IndexNode 703 - FOR doc IN geoFilter /* geo index scan */
- 5 ReturnNode 703 - RETURN doc
- Indexes used:
- By Type Collection Unique Sparse Selectivity Fields Ranges
- 6 geo geoFilter false true n/a [ `latitude`, `longitude` ] (GEO_DISTANCE([ 0, 0 ], [ doc.`longitude`, doc.`latitude` ]) < 2000)
- Optimization rules applied:
- Id RuleName
- 1 geo-index-optimizer
- 2 remove-unnecessary-calculations-2
- [
- {
- "_key" : "82990",
- "_id" : "geoFilter/82990",
- "_rev" : "_ZP4PXhu---",
- "name" : "Name/0/0",
- "latitude" : 0,
- "longitude" : 0
- }
- ]
- [object ArangoQueryCursor, count: 1, cached: false, hasMore: false]
Hide execution results
constructs a geo index selectioncollection.geo(location-attribute)
Looks up a geo index defined on attribute location_attribute.
Returns a geo index object if an index was found. The near
orwithin
operators can then be used to execute a geo-spatial query onthis particular index.
This is useful for collections with multiple defined geo indexes.
collection.geo(location_attribute, true)
Looks up a geo index on a compound attribute location_attribute.
Returns a geo index object if an index was found. The near
orwithin
operators can then be used to execute a geo-spatial query onthis particular index.
collection.geo(latitude_attribute, longitude_attribute)
Looks up a geo index defined on the two attributes latitude_attribute_and _longitude-attribute.
Returns a geo index object if an index was found. The near
orwithin
operators can then be used to execute a geo-spatial query onthis particular index.
Note: this method is not yet supported by the RocksDB storage engine.
Note: the geo simple query helper function is deprecated as of ArangoDB2.6. The function may be removed in future versions of ArangoDB. The preferredway for running geo queries is to use their AQL equivalents.
Examples
Assume you have a location stored as list in the attribute home_and a destination stored in the attribute _work. Then you can use thegeo
operator to select which geo-spatial attributes (and thus whichindex) to use in a near
query.
- arangosh> for (i = -90; i <= 90; i += 10) {
- ........> for (j = -180; j <= 180; j += 10) {
- ........> db.complex.save({ name : "Name/" + i + "/" + j,
- ........> home : [ i, j ],
- ........> work : [ -i, -j ] });
- ........> }
- ........> }
- ........>
- arangosh> db.complex.near(0, 170).limit(5);
- [ArangoError 1570: no suitable geo index found for geo restriction on 'complex']
- arangosh> db.complex.ensureIndex({ type: "geo", fields: [ "home" ] });
- {
- "bestIndexedLevel" : 17,
- "fields" : [
- "home"
- ],
- "geoJson" : false,
- "id" : "complex/85111",
- "isNewlyCreated" : true,
- "maxNumCoverCells" : 8,
- "sparse" : true,
- "type" : "geo",
- "unique" : false,
- "worstIndexedLevel" : 4,
- "code" : 201
- }
- arangosh> db.complex.near(0, 170).limit(5).toArray();
- [
- {
- "_key" : "84439",
- "_id" : "complex/84439",
- "_rev" : "_ZP4PX0y--A",
- "name" : "Name/0/170",
- "home" : [
- 0,
- 170
- ],
- "work" : [
- 0,
- -170
- ]
- },
- {
- "_key" : "84441",
- "_id" : "complex/84441",
- "_rev" : "_ZP4PX0y--C",
- "name" : "Name/0/180",
- "home" : [
- 0,
- 180
- ],
- "work" : [
- 0,
- -180
- ]
- },
- {
- "_key" : "84513",
- "_id" : "complex/84513",
- "_rev" : "_ZP4PX1q--A",
- "name" : "Name/10/170",
- "home" : [
- 10,
- 170
- ],
- "work" : [
- -10,
- -170
- ]
- },
- {
- "_key" : "84365",
- "_id" : "complex/84365",
- "_rev" : "_ZP4PXz2--C",
- "name" : "Name/-10/170",
- "home" : [
- -10,
- 170
- ],
- "work" : [
- 10,
- -170
- ]
- },
- {
- "_key" : "84369",
- "_id" : "complex/84369",
- "_rev" : "_ZP4PXz6--A",
- "name" : "Name/0/-180",
- "home" : [
- 0,
- -180
- ],
- "work" : [
- 0,
- 180
- ]
- }
- ]
- arangosh> db.complex.geo("work").near(0, 170).limit(5);
- [ArangoError 1570: no suitable geo index found for geo restriction on 'complex']
- arangosh> db.complex.ensureIndex({ type: "geo", fields: [ "work" ] });
- {
- "bestIndexedLevel" : 17,
- "fields" : [
- "work"
- ],
- "geoJson" : false,
- "id" : "complex/85119",
- "isNewlyCreated" : true,
- "maxNumCoverCells" : 8,
- "sparse" : true,
- "type" : "geo",
- "unique" : false,
- "worstIndexedLevel" : 4,
- "code" : 201
- }
- arangosh> db.complex.geo("work").near(0, 170).limit(5).toArray();
- [
- {
- "_key" : "84439",
- "_id" : "complex/84439",
- "_rev" : "_ZP4PX0y--A",
- "name" : "Name/0/170",
- "home" : [
- 0,
- 170
- ],
- "work" : [
- 0,
- -170
- ]
- },
- {
- "_key" : "84441",
- "_id" : "complex/84441",
- "_rev" : "_ZP4PX0y--C",
- "name" : "Name/0/180",
- "home" : [
- 0,
- 180
- ],
- "work" : [
- 0,
- -180
- ]
- },
- {
- "_key" : "84513",
- "_id" : "complex/84513",
- "_rev" : "_ZP4PX1q--A",
- "name" : "Name/10/170",
- "home" : [
- 10,
- 170
- ],
- "work" : [
- -10,
- -170
- ]
- },
- {
- "_key" : "84365",
- "_id" : "complex/84365",
- "_rev" : "_ZP4PXz2--C",
- "name" : "Name/-10/170",
- "home" : [
- -10,
- 170
- ],
- "work" : [
- 10,
- -170
- ]
- },
- {
- "_key" : "84369",
- "_id" : "complex/84369",
- "_rev" : "_ZP4PXz6--A",
- "name" : "Name/0/-180",
- "home" : [
- 0,
- -180
- ],
- "work" : [
- 0,
- 180
- ]
- }
- ]
Hide execution results
- arangosh> for (i = -90; i <= 90; i += 10) {
- ........> for (j = -180; j <= 180; j += 10) {
- ........> db.complex.save({ name : "Name/" + i + "/" + j,
- ........> home : [ i, j ],
- ........> work : [ -i, -j ] });
- ........> }
- ........> }
- ........>
- arangosh> db.complex.near(0, 170).limit(5);
- arangosh> db.complex.ensureIndex({ type: "geo", fields: [ "home" ] });
- arangosh> db.complex.near(0, 170).limit(5).toArray();
- arangosh> db.complex.geo("work").near(0, 170).limit(5);
- arangosh> db.complex.ensureIndex({ type: "geo", fields: [ "work" ] });
- arangosh> db.complex.geo("work").near(0, 170).limit(5).toArray();
Show execution results
constructs a near query for a collectioncollection.near(latitude, longitude)
The returned list is sorted according to the distance, with the nearestdocument to the coordinate (latitude, longitude) coming first.If there are near documents of equal distance, documents are chosen randomlyfrom this set until the limit is reached. It is possible to change the limitusing the limit operator.
In order to use the near operator, a geo index must be defined for thecollection. This index also defines which attribute holds the coordinatesfor the document. If you have more then one geo-spatial index, you can usethe geo operator to select a particular index.
Note: near
does not support negative skips.// However, you can still use limit
followed to skip.
collection.near(latitude, longitude).limit(limit)
Limits the result to limit documents instead of the default 100.
Note: Unlike with multiple explicit limits, limit
will raisethe implicit default limit imposed by within
.
collection.near(latitude, longitude).distance()
This will add an attribute distance
to all documents returned, whichcontains the distance between the given point and the document in meters.
collection.near(latitude, longitude).distance(name)
This will add an attribute name to all documents returned, whichcontains the distance between the given point and the document in meters.
Note: this method is not yet supported by the RocksDB storage engine.
Note: the near simple query function is deprecated as of ArangoDB 2.6.The function may be removed in future versions of ArangoDB. The preferredway for retrieving documents from a collection using the near operator isto use the AQL NEAR function in an AQL query as follows:
FOR doc IN NEAR(@@collection, @latitude, @longitude, @limit)
RETURN doc
Examples
To get the nearest two locations:
- arangosh> db.geo.ensureIndex({ type: "geo", fields: [ "loc" ] });
- {
- "bestIndexedLevel" : 17,
- "fields" : [
- "loc"
- ],
- "geoJson" : false,
- "id" : "geo/223",
- "isNewlyCreated" : true,
- "maxNumCoverCells" : 8,
- "sparse" : true,
- "type" : "geo",
- "unique" : false,
- "worstIndexedLevel" : 4,
- "code" : 201
- }
- arangosh> for (var i = -90; i <= 90; i += 10) {
- ........> for (var j = -180; j <= 180; j += 10) {
- ........> db.geo.save({
- ........> name : "Name/" + i + "/" + j,
- ........> loc: [ i, j ] });
- ........> } }
- arangosh> db.geo.near(0, 0).limit(2).toArray();
- [
- {
- "_key" : "927",
- "_id" : "geo/927",
- "_rev" : "_ZP4O6ba--A",
- "name" : "Name/0/0",
- "loc" : [
- 0,
- 0
- ]
- },
- {
- "_key" : "1001",
- "_id" : "geo/1001",
- "_rev" : "_ZP4O6cW--A",
- "name" : "Name/10/0",
- "loc" : [
- 10,
- 0
- ]
- }
- ]
Hide execution results
- arangosh> db.geo.ensureIndex({ type: "geo", fields: [ "loc" ] });
- arangosh> for (var i = -90; i <= 90; i += 10) {
- ........> for (var j = -180; j <= 180; j += 10) {
- ........> db.geo.save({
- ........> name : "Name/" + i + "/" + j,
- ........> loc: [ i, j ] });
- ........> } }
- arangosh> db.geo.near(0, 0).limit(2).toArray();
Show execution results
If you need the distance as well, then you can use the distance
operator:
- arangosh> db.geo.ensureIndex({ type: "geo", fields: [ "loc" ] });
- {
- "bestIndexedLevel" : 17,
- "fields" : [
- "loc"
- ],
- "geoJson" : false,
- "id" : "geo/1643",
- "isNewlyCreated" : true,
- "maxNumCoverCells" : 8,
- "sparse" : true,
- "type" : "geo",
- "unique" : false,
- "worstIndexedLevel" : 4,
- "code" : 201
- }
- arangosh> for (var i = -90; i <= 90; i += 10) {
- ........> for (var j = -180; j <= 180; j += 10) {
- ........> db.geo.save({
- ........> name : "Name/" + i + "/" + j,
- ........> loc: [ i, j ] });
- ........> } }
- arangosh> db.geo.near(0, 0).distance().limit(2).toArray();
- [
- {
- "_id" : "geo/2347",
- "_key" : "2347",
- "_rev" : "_ZP4O6u---D",
- "loc" : [
- 0,
- 0
- ],
- "name" : "Name/0/0",
- "distance" : 0
- },
- {
- "_id" : "geo/2421",
- "_key" : "2421",
- "_rev" : "_ZP4O6u2--A",
- "loc" : [
- 10,
- 0
- ],
- "name" : "Name/10/0",
- "distance" : 1111949.2664455872
- }
- ]
Hide execution results
- arangosh> db.geo.ensureIndex({ type: "geo", fields: [ "loc" ] });
- arangosh> for (var i = -90; i <= 90; i += 10) {
- ........> for (var j = -180; j <= 180; j += 10) {
- ........> db.geo.save({
- ........> name : "Name/" + i + "/" + j,
- ........> loc: [ i, j ] });
- ........> } }
- arangosh> db.geo.near(0, 0).distance().limit(2).toArray();
Show execution results
constructs a within query for a collectioncollection.within(latitude, longitude, radius)
This will find all documents within a given radius around the coordinate(latitude, longitude). The returned array is sorted by distance,beginning with the nearest document.
In order to use the within operator, a geo index must be defined for thecollection. This index also defines which attribute holds the coordinatesfor the document. If you have more then one geo-spatial index, you can usethe geo
operator to select a particular index.
collection.within(latitude, longitude, radius).distance()
This will add an attribute _distance
to all documents returned, whichcontains the distance between the given point and the document in meters.
collection.within(latitude, longitude, radius).distance(name)
This will add an attribute name to all documents returned, whichcontains the distance between the given point and the document in meters.
Note: this method is not yet supported by the RocksDB storage engine.
Note: the within simple query function is deprecated as of ArangoDB 2.6.The function may be removed in future versions of ArangoDB. The preferredway for retrieving documents from a collection using the within operator isto use the AQL WITHIN function in an AQL query as follows:
FOR doc IN WITHIN(@@collection, @latitude, @longitude, @radius, @distanceAttributeName)
RETURN doc
Examples
To find all documents within a radius of 2000 km use:
- arangosh> for (var i = -90; i <= 90; i += 10) {
- ........> for (var j = -180; j <= 180; j += 10) {
- ........> db.geo.save({ name : "Name/" + i + "/" + j, loc: [ i, j ] }); } }
- arangosh> db.geo.within(0, 0, 2000 * 1000).distance().toArray();
- [
- {
- "_id" : "geo/3767",
- "_key" : "3767",
- "_rev" : "_ZP4O7Ai--A",
- "loc" : [
- 0,
- 0
- ],
- "name" : "Name/0/0",
- "distance" : 0
- },
- {
- "_id" : "geo/3841",
- "_key" : "3841",
- "_rev" : "_ZP4O7Ba--A",
- "loc" : [
- 10,
- 0
- ],
- "name" : "Name/10/0",
- "distance" : 1111949.2664455872
- },
- {
- "_id" : "geo/3769",
- "_key" : "3769",
- "_rev" : "_ZP4O7Ai--C",
- "loc" : [
- 0,
- 10
- ],
- "name" : "Name/0/10",
- "distance" : 1111949.2664455872
- },
- {
- "_id" : "geo/3693",
- "_key" : "3693",
- "_rev" : "_ZP4O7_u---",
- "loc" : [
- -10,
- 0
- ],
- "name" : "Name/-10/0",
- "distance" : 1111949.2664455872
- },
- {
- "_id" : "geo/3765",
- "_key" : "3765",
- "_rev" : "_ZP4O7Ai---",
- "loc" : [
- 0,
- -10
- ],
- "name" : "Name/0/-10",
- "distance" : 1111949.2664455872
- },
- {
- "_id" : "geo/3691",
- "_key" : "3691",
- "_rev" : "_ZP4O7_q--A",
- "loc" : [
- -10,
- -10
- ],
- "name" : "Name/-10/-10",
- "distance" : 1568520.556798576
- },
- {
- "_id" : "geo/3843",
- "_key" : "3843",
- "_rev" : "_ZP4O7Ba--C",
- "loc" : [
- 10,
- 10
- ],
- "name" : "Name/10/10",
- "distance" : 1568520.556798576
- },
- {
- "_id" : "geo/3839",
- "_key" : "3839",
- "_rev" : "_ZP4O7Ba---",
- "loc" : [
- 10,
- -10
- ],
- "name" : "Name/10/-10",
- "distance" : 1568520.556798576
- },
- {
- "_id" : "geo/3695",
- "_key" : "3695",
- "_rev" : "_ZP4O7_u--A",
- "loc" : [
- -10,
- 10
- ],
- "name" : "Name/-10/10",
- "distance" : 1568520.556798576
- }
- ]
Hide execution results
- arangosh> for (var i = -90; i <= 90; i += 10) {
- ........> for (var j = -180; j <= 180; j += 10) {
- ........> db.geo.save({ name : "Name/" + i + "/" + j, loc: [ i, j ] }); } }
- arangosh> db.geo.within(0, 0, 2000 * 1000).distance().toArray();
Show execution results
ensures that a geo index existscollection.ensureIndex({ type: "geo", fields: [ "location" ] })
Since ArangoDB 2.5, this method is an alias for ensureGeoIndex sincegeo indexes are always sparse, meaning that documents that do not containthe index attributes or has non-numeric values in the index attributeswill not be indexed. ensureGeoConstraint is deprecated and _ensureGeoIndex_should be used instead.
The index does not provide a unique
option because of its limited usability.It would prevent identical coordinates from being inserted only, but even aslightly different location (like 1 inch or 1 cm off) would be unique again andnot considered a duplicate, although it probably should. The desired thresholdfor detecting duplicates may vary for every project (including how to calculatethe distance even) and needs to be implemented on the application layer asneeded. You can write a Foxx service for this purpose andmake use of the AQL geo functions to find nearbycoordinates supported by a geo index.