Geo Queries
It is recommended to use AQL instead, see Geo functions.
The ArangoDB allows to select documents based on geographic coordinates. Inorder for this to work, a geo-spatial index must be defined. This index willuse a very elaborate algorithm to lookup neighbors that is a magnitude fasterthan a simple R* index.
In general a geo coordinate is a pair of latitude and longitude, which mustboth be specified as numbers. A geo index can be created on coordinates thatare stored in a single list attribute with two elements like [-10, +30] (latitude first, followed by longitude) or on coordinates stored in two separate attributes.
For example, to index the following documents, an index can be created on theposition attribute of the documents:
db.test.save({ position: [ -10, 30 ] });
db.test.save({ position: [ 10, 45.5 ] });
db.test.ensureIndex({ type: "geo", fields: [ "position" ] });
If coordinates are stored in two distinct attributes, the index must be createdon the two attributes:
db.test.save({ latitude: -10, longitude: 30 });
db.test.save({ latitude: 10, longitude: 45.5 });
db.test.ensureIndex({ type: "geo", fields: [ "latitude", "longitude" ] });
In order to find all documents within a given radius around a coordinate use the within operator. In order to find all documents near a given document use the near operator.
It is possible to define more than one geo-spatial index per collection. Inthis case you must give a hint using the geo operator which of indexesshould be used in a query.
Near
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/215",
- "isNewlyCreated" : true,
- "maxNumCoverCells" : 8,
- "name" : "idx_1655125937813979136",
- "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" : "921",
- "_id" : "geo/921",
- "_rev" : "_Z2KCT1O--C",
- "name" : "Name/0/0",
- "loc" : [
- 0,
- 0
- ]
- },
- {
- "_key" : "995",
- "_id" : "geo/995",
- "_rev" : "_Z2KCT2O--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/1636",
- "isNewlyCreated" : true,
- "maxNumCoverCells" : 8,
- "name" : "idx_1655125938139037698",
- "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/2342",
- "_key" : "2342",
- "_rev" : "_Z2KCUIK--A",
- "loc" : [
- 0,
- 0
- ],
- "name" : "Name/0/0",
- "distance" : 0
- },
- {
- "_id" : "geo/2416",
- "_key" : "2416",
- "_rev" : "_Z2KCUJG---",
- "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
Within
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/3763",
- "_key" : "3763",
- "_rev" : "_Z2KCUau--C",
- "loc" : [
- 0,
- 0
- ],
- "name" : "Name/0/0",
- "distance" : 0
- },
- {
- "_id" : "geo/3837",
- "_key" : "3837",
- "_rev" : "_Z2KCUbq--C",
- "loc" : [
- 10,
- 0
- ],
- "name" : "Name/10/0",
- "distance" : 1111949.2664455872
- },
- {
- "_id" : "geo/3765",
- "_key" : "3765",
- "_rev" : "_Z2KCUay---",
- "loc" : [
- 0,
- 10
- ],
- "name" : "Name/0/10",
- "distance" : 1111949.2664455872
- },
- {
- "_id" : "geo/3689",
- "_key" : "3689",
- "_rev" : "_Z2KCUZy--C",
- "loc" : [
- -10,
- 0
- ],
- "name" : "Name/-10/0",
- "distance" : 1111949.2664455872
- },
- {
- "_id" : "geo/3761",
- "_key" : "3761",
- "_rev" : "_Z2KCUau--A",
- "loc" : [
- 0,
- -10
- ],
- "name" : "Name/0/-10",
- "distance" : 1111949.2664455872
- },
- {
- "_id" : "geo/3687",
- "_key" : "3687",
- "_rev" : "_Z2KCUZy--A",
- "loc" : [
- -10,
- -10
- ],
- "name" : "Name/-10/-10",
- "distance" : 1568520.556798576
- },
- {
- "_id" : "geo/3839",
- "_key" : "3839",
- "_rev" : "_Z2KCUbu---",
- "loc" : [
- 10,
- 10
- ],
- "name" : "Name/10/10",
- "distance" : 1568520.556798576
- },
- {
- "_id" : "geo/3835",
- "_key" : "3835",
- "_rev" : "_Z2KCUbq--A",
- "loc" : [
- 10,
- -10
- ],
- "name" : "Name/10/-10",
- "distance" : 1568520.556798576
- },
- {
- "_id" : "geo/3691",
- "_key" : "3691",
- "_rev" : "_Z2KCUZ2---",
- "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
Geo
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/86125",
- "isNewlyCreated" : true,
- "maxNumCoverCells" : 8,
- "name" : "idx_1655126005418819585",
- "sparse" : true,
- "type" : "geo",
- "unique" : false,
- "worstIndexedLevel" : 4,
- "code" : 201
- }
- arangosh> db.complex.near(0, 170).limit(5).toArray();
- [
- {
- "_key" : "85454",
- "_id" : "complex/85454",
- "_rev" : "_Z2KDSjO--A",
- "name" : "Name/0/170",
- "home" : [
- 0,
- 170
- ],
- "work" : [
- 0,
- -170
- ]
- },
- {
- "_key" : "85456",
- "_id" : "complex/85456",
- "_rev" : "_Z2KDSjO--C",
- "name" : "Name/0/180",
- "home" : [
- 0,
- 180
- ],
- "work" : [
- 0,
- -180
- ]
- },
- {
- "_key" : "85528",
- "_id" : "complex/85528",
- "_rev" : "_Z2KDSj6--A",
- "name" : "Name/10/170",
- "home" : [
- 10,
- 170
- ],
- "work" : [
- -10,
- -170
- ]
- },
- {
- "_key" : "85380",
- "_id" : "complex/85380",
- "_rev" : "_Z2KDSii--C",
- "name" : "Name/-10/170",
- "home" : [
- -10,
- 170
- ],
- "work" : [
- 10,
- -170
- ]
- },
- {
- "_key" : "85384",
- "_id" : "complex/85384",
- "_rev" : "_Z2KDSim---",
- "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/86134",
- "isNewlyCreated" : true,
- "maxNumCoverCells" : 8,
- "name" : "idx_1655126005425111040",
- "sparse" : true,
- "type" : "geo",
- "unique" : false,
- "worstIndexedLevel" : 4,
- "code" : 201
- }
- arangosh> db.complex.geo("work").near(0, 170).limit(5).toArray();
- [
- {
- "_key" : "85454",
- "_id" : "complex/85454",
- "_rev" : "_Z2KDSjO--A",
- "name" : "Name/0/170",
- "home" : [
- 0,
- 170
- ],
- "work" : [
- 0,
- -170
- ]
- },
- {
- "_key" : "85456",
- "_id" : "complex/85456",
- "_rev" : "_Z2KDSjO--C",
- "name" : "Name/0/180",
- "home" : [
- 0,
- 180
- ],
- "work" : [
- 0,
- -180
- ]
- },
- {
- "_key" : "85528",
- "_id" : "complex/85528",
- "_rev" : "_Z2KDSj6--A",
- "name" : "Name/10/170",
- "home" : [
- 10,
- 170
- ],
- "work" : [
- -10,
- -170
- ]
- },
- {
- "_key" : "85380",
- "_id" : "complex/85380",
- "_rev" : "_Z2KDSii--C",
- "name" : "Name/-10/170",
- "home" : [
- -10,
- 170
- ],
- "work" : [
- 10,
- -170
- ]
- },
- {
- "_key" : "85384",
- "_id" : "complex/85384",
- "_rev" : "_Z2KDSim---",
- "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
Related topics
Other ArangoDB geographic features are described in: