ArangoDB v3.8 reached End of Life (EOL) and is no longer supported.

This documentation is outdated. Please see the most recent version at docs.arangodb.com

Sequential Access and Cursors

If a query returns a cursor (for example by calling db._query(...)), then you can use hasNext and next to iterate over the result set or toArray to convert it to an array.

If the number of query results is expected to be big, it is possible to limit the amount of documents transferred between the server and the client to a specific value. This value is called batchSize. The batchSize can optionally be set when a query is executed using its execute method. If no batchSize value is specified, the server will pick a reasonable default value. If the server has more documents than should be returned in a single batch, the server will set the hasMore attribute in the result. It will also return the id of the server-side cursor in the id attribute in the result. This id can be used with the cursor API to fetch any outstanding results from the server and dispose the server-side cursor afterwards.

Has Next

checks if the cursor is exhausted cursor.hasNext()

The hasNext operator returns true, then the cursor still has documents. In this case the next document can be accessed using the next operator, which will advance the cursor.

Examples

arangosh> var a = db._query("FOR x IN five RETURN x");
arangosh> while (a.hasNext()) print(a.next());
Show execution results
Hide execution results
{ 
  "_key" : "71339", 
  "_id" : "five/71339", 
  "_rev" : "_fw2Y2Bq--_", 
  "name" : "one" 
}
{ 
  "_key" : "71341", 
  "_id" : "five/71341", 
  "_rev" : "_fw2Y2By---", 
  "name" : "two" 
}
{ 
  "_key" : "71343", 
  "_id" : "five/71343", 
  "_rev" : "_fw2Y2By--_", 
  "name" : "three" 
}
{ 
  "_key" : "71345", 
  "_id" : "five/71345", 
  "_rev" : "_fw2Y2By--A", 
  "name" : "four" 
}
{ 
  "_key" : "71347", 
  "_id" : "five/71347", 
  "_rev" : "_fw2Y2By--B", 
  "name" : "five" 
}

Next

returns the next result document cursor.next()

If the hasNext operator returns true, then the underlying cursor of the AQL query still has documents. In this case the next document can be accessed using the next operator, which will advance the underlying cursor. If you use next on an exhausted cursor, then undefined is returned.

Examples

arangosh> db._query("FOR x IN five RETURN x").next();
Show execution results
Hide execution results
{ 
  "_key" : "71358", 
  "_id" : "five/71358", 
  "_rev" : "_fw2Y2CC--_", 
  "name" : "one" 
}

Execute Query

executes a query query.execute(batchSize)

Executes an AQL query. If the optional batchSize value is specified, the server will return at most batchSize values in one roundtrip. The batchSize cannot be adjusted after the query is first executed.

Note: There is no need to explicitly call the execute method if another means of fetching the query results is chosen. The following two approaches lead to the same result:

arangosh> var result = db.users.all().toArray();
........> print(result);
........> var q = db._query("FOR x IN users RETURN x");
........> result = [ ];
........> while (q.hasNext()) {
........>   result.push(q.next());
........> }
........> print(result);
Show execution results
Hide execution results
[ 
  { 
    "_key" : "72054", 
    "_id" : "users/72054", 
    "_rev" : "_fw2Y2Im--_", 
    "name" : "Gerhard" 
  }, 
  { 
    "_key" : "72056", 
    "_id" : "users/72056", 
    "_rev" : "_fw2Y2Iq---", 
    "name" : "Helmut" 
  }, 
  { 
    "_key" : "72058", 
    "_id" : "users/72058", 
    "_rev" : "_fw2Y2Iq--_", 
    "name" : "Angela" 
  } 
]
[ 
  { 
    "_key" : "72054", 
    "_id" : "users/72054", 
    "_rev" : "_fw2Y2Im--_", 
    "name" : "Gerhard" 
  }, 
  { 
    "_key" : "72056", 
    "_id" : "users/72056", 
    "_rev" : "_fw2Y2Iq---", 
    "name" : "Helmut" 
  }, 
  { 
    "_key" : "72058", 
    "_id" : "users/72058", 
    "_rev" : "_fw2Y2Iq--_", 
    "name" : "Angela" 
  } 
]

The following two alternatives both use a batch size and return the same result:

arangosh> var result = [ ];
........> var q = db.users.all();
........> q.execute(1);
........> while(q.hasNext()) {
........>   result.push(q.next());
........> }
........> print(result);
........> result = [ ];
........> q = db._query("FOR x IN users RETURN x", {}, { batchSize: 1 });
........> while (q.hasNext()) {
........>   result.push(q.next());
........> }
........> print(result);
Show execution results
Hide execution results
[ 
  { 
    "_key" : "72035", 
    "_id" : "users/72035", 
    "_rev" : "_fw2Y2Ia--_", 
    "name" : "Gerhard" 
  }, 
  { 
    "_key" : "72037", 
    "_id" : "users/72037", 
    "_rev" : "_fw2Y2Ia--A", 
    "name" : "Helmut" 
  }, 
  { 
    "_key" : "72039", 
    "_id" : "users/72039", 
    "_rev" : "_fw2Y2Ie---", 
    "name" : "Angela" 
  } 
]
[ 
  { 
    "_key" : "72035", 
    "_id" : "users/72035", 
    "_rev" : "_fw2Y2Ia--_", 
    "name" : "Gerhard" 
  }, 
  { 
    "_key" : "72037", 
    "_id" : "users/72037", 
    "_rev" : "_fw2Y2Ia--A", 
    "name" : "Helmut" 
  }, 
  { 
    "_key" : "72039", 
    "_id" : "users/72039", 
    "_rev" : "_fw2Y2Ie---", 
    "name" : "Angela" 
  } 
]

Dispose

disposes the result cursor.dispose()

If you are no longer interested in any further results, you should call dispose in order to free any resources associated with the cursor. After calling dispose you can no longer access the cursor.

Count

counts the number of documents cursor.count()

The count operator counts the number of document in the result set and returns that number. The count operator ignores any limits and returns the total number of documents found.

cursor.count(true)

If the result set was limited by the limit operator or documents were skipped using the skip operator, the count operator with argument true will use the number of elements in the final result set - after applying limit and skip.