Sequential Access and Cursors

If a query returns a cursor (for example by calling db.query(…)), then you can use _hasNext and next toiterate 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 clientto a specific value. This value is called batchSize. The batchSize_can optionally be set before or when a simple query is executed.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 alsoreturn 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 fromthe server and dispose the server-side cursor afterwards.

The initial batchSize value can be set using the setBatchSize_method that is available for each type of simple query, or when the simplequery is executed using its _execute method. If no batchSize valueis specified, the server will pick a reasonable default value.

Has Next

checks if the cursor is exhaustedcursor.hasNext()

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

Examples

  1. arangosh> var a = db._query("FOR x IN five RETURN x");
  2. arangosh> while (a.hasNext()) print(a.next());

Show execution results

  1. {
  2. "_key" : "73955",
  3. "_id" : "five/73955",
  4. "_rev" : "_ZJNS756--A",
  5. "name" : "one"
  6. }
  7. {
  8. "_key" : "73957",
  9. "_id" : "five/73957",
  10. "_rev" : "_ZJNS76----",
  11. "name" : "two"
  12. }
  13. {
  14. "_key" : "73959",
  15. "_id" : "five/73959",
  16. "_rev" : "_ZJNS76---A",
  17. "name" : "three"
  18. }
  19. {
  20. "_key" : "73961",
  21. "_id" : "five/73961",
  22. "_rev" : "_ZJNS76---C",
  23. "name" : "four"
  24. }
  25. {
  26. "_key" : "73963",
  27. "_id" : "five/73963",
  28. "_rev" : "_ZJNS76---E",
  29. "name" : "five"
  30. }

Hide execution results

Next

returns the next result documentcursor.next()

If the hasNext operator returns true, then the underlyingcursor of the simple query still has documents. In this case thenext document can be accessed using the next operator, whichwill advance the underlying cursor. If you use next on anexhausted cursor, then undefined is returned.

Examples

  1. arangosh> db._query("FOR x IN five RETURN x").next();

Show execution results

  1. {
  2. "_key" : "73973",
  3. "_id" : "five/73973",
  4. "_rev" : "_ZJNS76O--A",
  5. "name" : "one"
  6. }

Hide execution results

Set Batch size

sets the batch size for any following requestscursor.setBatchSize(number)

Sets the batch size for queries. The batch size determines how many resultsare at most transferred from the server to the client in one chunk.

Get Batch size

returns the batch sizecursor.getBatchSize()

Returns the batch size for queries. If the returned value is undefined, theserver will determine a sensible batch size for any following requests.

Execute Query

executes a queryquery.execute(batchSize)

Executes a simple 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 anothermeans of fetching the query results is chosen. The following two approacheslead to the same result:

  1. arangosh> result = db.users.all().toArray();
  2. arangosh> var q = db._query("FOR x IN users RETURN x");
  3. ........> result = [ ];
  4. ........> while (q.hasNext()) {
  5. ........> result.push(q.next());
  6. ........> }

Show execution results

  1. [
  2. {
  3. "_key" : "74873",
  4. "_id" : "users/74873",
  5. "_rev" : "_ZJNS8OK--A",
  6. "name" : "Gerhard"
  7. },
  8. {
  9. "_key" : "74875",
  10. "_id" : "users/74875",
  11. "_rev" : "_ZJNS8OO---",
  12. "name" : "Helmut"
  13. },
  14. {
  15. "_key" : "74877",
  16. "_id" : "users/74877",
  17. "_rev" : "_ZJNS8OO--A",
  18. "name" : "Angela"
  19. }
  20. ]

Hide execution results

The following two alternatives both use a batchSize and return the sameresult:

  1. arangosh> q = db.users.all(); q.setBatchSize(20); q.execute(); while (q.hasNext()) { print(q.next()); }
  2. arangosh> q = db.users.all(); q.execute(20); while (q.hasNext()) { print(q.next()); }

Show execution results

  1. {
  2. "_key" : "74856",
  3. "_id" : "users/74856",
  4. "_rev" : "_ZJNS8N2--A",
  5. "name" : "Gerhard"
  6. }
  7. {
  8. "_key" : "74858",
  9. "_id" : "users/74858",
  10. "_rev" : "_ZJNS8N2--C",
  11. "name" : "Helmut"
  12. }
  13. {
  14. "_key" : "74860",
  15. "_id" : "users/74860",
  16. "_rev" : "_ZJNS8N6---",
  17. "name" : "Angela"
  18. }
  19. SimpleQueryAll(users)
  20. {
  21. "_key" : "74856",
  22. "_id" : "users/74856",
  23. "_rev" : "_ZJNS8N2--A",
  24. "name" : "Gerhard"
  25. }
  26. {
  27. "_key" : "74858",
  28. "_id" : "users/74858",
  29. "_rev" : "_ZJNS8N2--C",
  30. "name" : "Helmut"
  31. }
  32. {
  33. "_key" : "74860",
  34. "_id" : "users/74860",
  35. "_rev" : "_ZJNS8N6---",
  36. "name" : "Angela"
  37. }
  38. SimpleQueryAll(users)

Hide execution results

Dispose

disposes the resultcursor.dispose()

If you are no longer interested in any further results, you should calldispose 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 documentscursor.count()

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

Note: Not all simple queries support counting. In this case null isreturned (Simple queries are deprecated).

cursor.count(true)

If the result set was limited by the limit operator or documents wereskiped using the skip operator, the count operator with argumenttrue will use the number of elements in the final result set - afterapplying limit and skip.

Note: Not all simple queries support counting. In this case null isreturned (Simple queries are deprecated)..