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
- arangosh> var a = db._query("FOR x IN five RETURN x");
- arangosh> while (a.hasNext()) print(a.next());
Show execution results
- {
- "_key" : "73337",
- "_id" : "five/73337",
- "_rev" : "_ZP4PTjK---",
- "name" : "one"
- }
- {
- "_key" : "73339",
- "_id" : "five/73339",
- "_rev" : "_ZP4PTjO---",
- "name" : "two"
- }
- {
- "_key" : "73341",
- "_id" : "five/73341",
- "_rev" : "_ZP4PTjO--A",
- "name" : "three"
- }
- {
- "_key" : "73343",
- "_id" : "five/73343",
- "_rev" : "_ZP4PTjS---",
- "name" : "four"
- }
- {
- "_key" : "73345",
- "_id" : "five/73345",
- "_rev" : "_ZP4PTjS--A",
- "name" : "five"
- }
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
- arangosh> db._query("FOR x IN five RETURN x").next();
Show execution results
- {
- "_key" : "73355",
- "_id" : "five/73355",
- "_rev" : "_ZP4PTjq---",
- "name" : "one"
- }
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:
- arangosh> result = db.users.all().toArray();
- arangosh> var q = db._query("FOR x IN users RETURN x");
- ........> result = [ ];
- ........> while (q.hasNext()) {
- ........> result.push(q.next());
- ........> }
Show execution results
- [
- {
- "_key" : "74029",
- "_id" : "users/74029",
- "_rev" : "_ZP4PUES---",
- "name" : "Gerhard"
- },
- {
- "_key" : "74031",
- "_id" : "users/74031",
- "_rev" : "_ZP4PUES--A",
- "name" : "Helmut"
- },
- {
- "_key" : "74033",
- "_id" : "users/74033",
- "_rev" : "_ZP4PUEW---",
- "name" : "Angela"
- }
- ]
Hide execution results
The following two alternatives both use a batchSize and return the sameresult:
- arangosh> q = db.users.all(); q.setBatchSize(20); q.execute(); while (q.hasNext()) { print(q.next()); }
- arangosh> q = db.users.all(); q.execute(20); while (q.hasNext()) { print(q.next()); }
Show execution results
- {
- "_key" : "74012",
- "_id" : "users/74012",
- "_rev" : "_ZP4PUDu--A",
- "name" : "Gerhard"
- }
- {
- "_key" : "74014",
- "_id" : "users/74014",
- "_rev" : "_ZP4PUDy---",
- "name" : "Helmut"
- }
- {
- "_key" : "74016",
- "_id" : "users/74016",
- "_rev" : "_ZP4PUDy--A",
- "name" : "Angela"
- }
- SimpleQueryAll(users)
- {
- "_key" : "74012",
- "_id" : "users/74012",
- "_rev" : "_ZP4PUDu--A",
- "name" : "Gerhard"
- }
- {
- "_key" : "74014",
- "_id" : "users/74014",
- "_rev" : "_ZP4PUDy---",
- "name" : "Helmut"
- }
- {
- "_key" : "74016",
- "_id" : "users/74016",
- "_rev" : "_ZP4PUDy--A",
- "name" : "Angela"
- }
- 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)..