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 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 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.

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

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

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

Show execution results

Hide execution results

  1. {
  2. "_key" : "75832",
  3. "_id" : "five/75832",
  4. "_rev" : "_dAzP086--A",
  5. "name" : "one"
  6. }
  7. {
  8. "_key" : "75834",
  9. "_id" : "five/75834",
  10. "_rev" : "_dAzP09----",
  11. "name" : "two"
  12. }
  13. {
  14. "_key" : "75836",
  15. "_id" : "five/75836",
  16. "_rev" : "_dAzP09---A",
  17. "name" : "three"
  18. }
  19. {
  20. "_key" : "75838",
  21. "_id" : "five/75838",
  22. "_rev" : "_dAzP09---C",
  23. "name" : "four"
  24. }
  25. {
  26. "_key" : "75840",
  27. "_id" : "five/75840",
  28. "_rev" : "_dAzP09---E",
  29. "name" : "five"
  30. }

Next

returns the next result document cursor.next()

If the hasNext operator returns true, then the underlying cursor of the simple 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

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

Show execution results

Hide execution results

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

Set Batch size

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

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

Get Batch size

returns the batch size cursor.getBatchSize()

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

Execute Query

executes a query query.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 another means of fetching the query results is chosen. The following two approaches lead 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

Hide execution results

  1. [
  2. {
  3. "_key" : "76699",
  4. "_id" : "users/76699",
  5. "_rev" : "_dAzP1R----",
  6. "name" : "Gerhard"
  7. },
  8. {
  9. "_key" : "76701",
  10. "_id" : "users/76701",
  11. "_rev" : "_dAzP1R---A",
  12. "name" : "Helmut"
  13. },
  14. {
  15. "_key" : "76703",
  16. "_id" : "users/76703",
  17. "_rev" : "_dAzP1RC---",
  18. "name" : "Angela"
  19. }
  20. ]

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

  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

Hide execution results

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

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.

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

cursor.count(true)

If the result set was limited by the limit operator or documents were skiped 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.

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