1
1
---
2
2
layout : default
3
- description : Within the ArangoDB shell, the _query and _createStatement methods of thedb object can be used to execute AQL queries
3
+ description : Within the ArangoDB shell, the _query and _createStatement methods of the db object can be used to execute AQL queries
4
4
---
5
5
Executing queries from Arangosh
6
6
===============================
@@ -16,6 +16,7 @@ One can execute queries with the *_query* method of the *db* object.
16
16
This will run the specified query in the context of the currently
17
17
selected database and return the query results in a cursor. The results of the cursor
18
18
can be printed using its * toArray* method:
19
+
19
20
{% arangoshexample examplevar="examplevar" script="script" result="result" %}
20
21
@startDocuBlockInline 01_workWithAQL_all
21
22
@EXAMPLE_ARANGOSH_OUTPUT{01_workWithAQL_all}
@@ -27,10 +28,12 @@ can be printed using its *toArray* method:
27
28
@endDocuBlock 01_workWithAQL_all
28
29
{% endarangoshexample %}
29
30
{% include arangoshexample.html id=examplevar script=script result=result %}
31
+
30
32
### db._ query Bind parameters
31
33
32
34
To pass bind parameters into a query, they can be specified as second argument to the
33
35
* _ query* method:
36
+
34
37
{% arangoshexample examplevar="examplevar" script="script" result="result" %}
35
38
@startDocuBlockInline 02_workWithAQL_bindValues
36
39
@EXAMPLE_ARANGOSH_OUTPUT{02_workWithAQL_bindValues}
@@ -43,6 +46,7 @@ To pass bind parameters into a query, they can be specified as second argument t
43
46
@endDocuBlock 02_workWithAQL_bindValues
44
47
{% endarangoshexample %}
45
48
{% include arangoshexample.html id=examplevar script=script result=result %}
49
+
46
50
### ES6 template strings
47
51
48
52
It is also possible to use ES6 template strings for generating AQL queries. There is
@@ -59,6 +63,7 @@ aql`FOR c IN mycollection FILTER c._key == ${key} RETURN c._key`;
59
63
}
60
64
}
61
65
```
66
+
62
67
{% arangoshexample examplevar="examplevar" script="script" result="result" %}
63
68
@startDocuBlockInline 02_workWithAQL_aqlQuery
64
69
@EXAMPLE_ARANGOSH_OUTPUT{02_workWithAQL_aqlQuery}
@@ -70,8 +75,10 @@ aql`FOR c IN mycollection FILTER c._key == ${key} RETURN c._key`;
70
75
@endDocuBlock 02_workWithAQL_aqlQuery
71
76
{% endarangoshexample %}
72
77
{% include arangoshexample.html id=examplevar script=script result=result %}
73
- Arbitrary JavaScript expressions can be used in queries that are generated with the
78
+
79
+ Arbitrary JavaScript expressions can be used in queries that are generated with the
74
80
* aql* template string generator. Collection objects are handled automatically:
81
+
75
82
{% arangoshexample examplevar="examplevar" script="script" result="result" %}
76
83
@startDocuBlockInline 02_workWithAQL_aqlCollectionQuery
77
84
@EXAMPLE_ARANGOSH_OUTPUT{02_workWithAQL_aqlCollectionQuery}
@@ -82,13 +89,15 @@ Arbitrary JavaScript expressions can be used in queries that are generated with
82
89
@endDocuBlock 02_workWithAQL_aqlCollectionQuery
83
90
{% endarangoshexample %}
84
91
{% include arangoshexample.html id=examplevar script=script result=result %}
92
+
85
93
Note: data-modification AQL queries normally do not return a result (unless the AQL query
86
94
contains an extra * RETURN* statement). When not using a * RETURN* statement in the query, the
87
95
* toArray* method will return an empty array.
88
96
89
97
### Statistics and extra Information
90
-
98
+
91
99
It is always possible to retrieve statistics for a query with the * getExtra* method:
100
+
92
101
{% arangoshexample examplevar="examplevar" script="script" result="result" %}
93
102
@startDocuBlockInline 03_workWithAQL_getExtra
94
103
@EXAMPLE_ARANGOSH_OUTPUT{03_workWithAQL_getExtra}
@@ -100,6 +109,7 @@ It is always possible to retrieve statistics for a query with the *getExtra* met
100
109
@endDocuBlock 03_workWithAQL_getExtra
101
110
{% endarangoshexample %}
102
111
{% include arangoshexample.html id=examplevar script=script result=result %}
112
+
103
113
The meaning of the statistics values is described in [ Execution statistics] ( execution-and-performance-query-statistics.html ) .
104
114
You also will find warnings in here; If you're designing queries on the shell be sure to also look at it.
105
115
@@ -111,6 +121,7 @@ allowed to use. When a single AQL query reaches the specified limit value,
111
121
the query will be aborted with a * resource limit exceeded* exception. In a
112
122
cluster, the memory accounting is done per shard, so the limit value is
113
123
effectively a memory limit per query per shard.
124
+
114
125
{% arangoshexample examplevar="examplevar" script="script" result="result" %}
115
126
@startDocuBlockInline 02_workWithAQL_memoryLimit
116
127
@EXAMPLE_ARANGOSH_OUTPUT{02_workWithAQL_memoryLimit}
@@ -122,6 +133,7 @@ effectively a memory limit per query per shard.
122
133
@endDocuBlock 02_workWithAQL_memoryLimit
123
134
{% endarangoshexample %}
124
135
{% include arangoshexample.html id=examplevar script=script result=result %}
136
+
125
137
If no memory limit is specified, then the server default value (controlled by
126
138
startup option * --query.memory-limit* will be used for restricting the maximum amount
127
139
of memory the query can use. A memory limit value of * 0* means that the maximum
@@ -170,6 +182,9 @@ There are further options that can be passed in the *options* attribute of the *
170
182
after the query is finished.
171
183
The default value is * false*
172
184
185
+ - * timeout* : The query has to be executed within the given timeout or it will be killed.
186
+ The value is specified in seconds. The default value is * 0.0* (no timeout).
187
+
173
188
The following additional attributes can be passed to queries in the RocksDB storage engine:
174
189
175
190
- * maxTransactionSize* : transaction size limit in bytes
@@ -196,6 +211,7 @@ The *_query* method is a shorthand for creating an ArangoStatement object,
196
211
executing it and iterating over the resulting cursor. If more control over the
197
212
result set iteration is needed, it is recommended to first create an
198
213
ArangoStatement object as follows:
214
+
199
215
{% arangoshexample examplevar="examplevar" script="script" result="result" %}
200
216
@startDocuBlockInline 04_workWithAQL_statements1
201
217
@EXAMPLE_ARANGOSH_OUTPUT{04_workWithAQL_statements1}
@@ -205,7 +221,9 @@ ArangoStatement object as follows:
205
221
@endDocuBlock 04_workWithAQL_statements1
206
222
{% endarangoshexample %}
207
223
{% include arangoshexample.html id=examplevar script=script result=result %}
224
+
208
225
To execute the query, use the * execute* method of the statement:
226
+
209
227
{% arangoshexample examplevar="examplevar" script="script" result="result" %}
210
228
@startDocuBlockInline 05_workWithAQL_statements2
211
229
@EXAMPLE_ARANGOSH_OUTPUT{05_workWithAQL_statements2}
@@ -215,12 +233,14 @@ To execute the query, use the *execute* method of the statement:
215
233
@endDocuBlock 05_workWithAQL_statements2
216
234
{% endarangoshexample %}
217
235
{% include arangoshexample.html id=examplevar script=script result=result %}
236
+
218
237
### Cursors
219
238
220
239
Once the query executed the query results are available in a cursor.
221
240
The cursor can return all its results at once using the * toArray* method.
222
241
This is a short-cut that you can use if you want to access the full result
223
242
set without iterating over it yourself.
243
+
224
244
{% arangoshexample examplevar="examplevar" script="script" result="result" %}
225
245
@startDocuBlockInline 05_workWithAQL_statements3
226
246
@EXAMPLE_ARANGOSH_OUTPUT{05_workWithAQL_statements3}
@@ -232,7 +252,6 @@ set without iterating over it yourself.
232
252
{% endarangoshexample %}
233
253
{% include arangoshexample.html id=examplevar script=script result=result %}
234
254
235
-
236
255
Cursors can also be used to iterate over the result set document-by-document.
237
256
To do so, use the * hasNext* and * next* methods of the cursor:
238
257
{% arangoshexample examplevar="examplevar" script="script" result="result" %}
@@ -245,17 +264,19 @@ To do so, use the *hasNext* and *next* methods of the cursor:
245
264
@endDocuBlock 05_workWithAQL_statements4
246
265
{% endarangoshexample %}
247
266
{% include arangoshexample.html id=examplevar script=script result=result %}
267
+
248
268
Please note that you can iterate over the results of a cursor only once, and that
249
269
the cursor will be empty when you have fully iterated over it. To iterate over
250
270
the results again, the query needs to be re-executed.
251
-
271
+
252
272
Additionally, the iteration can be done in a forward-only fashion. There is no
253
273
backwards iteration or random access to elements in a cursor.
254
274
255
275
### ArangoStatement parameters binding
256
276
257
277
To execute an AQL query using bind parameters, you need to create a statement first
258
278
and then bind the parameters to it before execution:
279
+
259
280
{% arangoshexample examplevar="examplevar" script="script" result="result" %}
260
281
@startDocuBlockInline 05_workWithAQL_statements5
261
282
@EXAMPLE_ARANGOSH_OUTPUT{05_workWithAQL_statements5}
@@ -268,8 +289,10 @@ and then bind the parameters to it before execution:
268
289
@endDocuBlock 05_workWithAQL_statements5
269
290
{% endarangoshexample %}
270
291
{% include arangoshexample.html id=examplevar script=script result=result %}
292
+
271
293
The cursor results can then be dumped or iterated over as usual, e.g.:
272
- {% arangoshexample examplevar="examplevar" script="script" result="result" %}
294
+
295
+ {% arangoshexample examplevar="examplevar" script="script" result="result" %}
273
296
@startDocuBlockInline 05_workWithAQL_statements6
274
297
@EXAMPLE_ARANGOSH_OUTPUT{05_workWithAQL_statements6}
275
298
~ var stmt = db._ createStatement( { "query": "FOR i IN [ @one , @two ] RETURN i * 2" } );
@@ -281,7 +304,9 @@ The cursor results can then be dumped or iterated over as usual, e.g.:
281
304
@endDocuBlock 05_workWithAQL_statements6
282
305
{% endarangoshexample %}
283
306
{% include arangoshexample.html id=examplevar script=script result=result %}
284
- or
307
+
308
+ or
309
+
285
310
{% arangoshexample examplevar="examplevar" script="script" result="result" %}
286
311
@startDocuBlockInline 05_workWithAQL_statements7
287
312
@EXAMPLE_ARANGOSH_OUTPUT{05_workWithAQL_statements7}
294
319
@endDocuBlock 05_workWithAQL_statements7
295
320
{% endarangoshexample %}
296
321
{% include arangoshexample.html id=examplevar script=script result=result %}
322
+
297
323
Please note that bind parameters can also be passed into the * _ createStatement* method directly,
298
324
making it a bit more convenient:
299
- {% arangoshexample examplevar="examplevar" script="script" result="result" %}
325
+
326
+ {% arangoshexample examplevar="examplevar" script="script" result="result" %}
300
327
@startDocuBlockInline 05_workWithAQL_statements8
301
328
@EXAMPLE_ARANGOSH_OUTPUT{05_workWithAQL_statements8}
302
329
|stmt = db._ createStatement( {
@@ -310,11 +337,13 @@ making it a bit more convenient:
310
337
@endDocuBlock 05_workWithAQL_statements8
311
338
{% endarangoshexample %}
312
339
{% include arangoshexample.html id=examplevar script=script result=result %}
340
+
313
341
### Counting with a cursor
314
-
342
+
315
343
Cursors also optionally provide the total number of results. By default, they do not.
316
344
To make the server return the total number of results, you may set the * count* attribute to
317
345
* true* when creating a statement:
346
+
318
347
{% arangoshexample examplevar="examplevar" script="script" result="result" %}
319
348
@startDocuBlockInline 05_workWithAQL_statements9
320
349
@EXAMPLE_ARANGOSH_OUTPUT{05_workWithAQL_statements9}
@@ -325,8 +354,10 @@ To make the server return the total number of results, you may set the *count* a
325
354
@endDocuBlock 05_workWithAQL_statements9
326
355
{% endarangoshexample %}
327
356
{% include arangoshexample.html id=examplevar script=script result=result %}
357
+
328
358
After executing this query, you can use the * count* method of the cursor to get the
329
359
number of total results from the result set:
360
+
330
361
{% arangoshexample examplevar="examplevar" script="script" result="result" %}
331
362
@startDocuBlockInline 05_workWithAQL_statements10
332
363
@EXAMPLE_ARANGOSH_OUTPUT{05_workWithAQL_statements10}
@@ -337,6 +368,7 @@ number of total results from the result set:
337
368
@endDocuBlock 05_workWithAQL_statements10
338
369
{% endarangoshexample %}
339
370
{% include arangoshexample.html id=examplevar script=script result=result %}
371
+
340
372
Please note that the * count* method returns nothing if you did not specify the * count*
341
373
attribute when creating the query.
342
374
@@ -354,13 +386,13 @@ This may change in the future. Future versions of ArangoDB may create result set
354
386
on the server-side and may be able to apply optimizations if a result set is not fully fetched by
355
387
a client.
356
388
357
-
358
389
### Using cursors to obtain additional information on internal timings
359
390
360
391
Cursors can also optionally provide statistics of the internal execution phases. By default, they do not.
361
392
To get to know how long parsing, optimization, instantiation and execution took,
362
393
make the server return that by setting the * profile* attribute to
363
394
* true* when creating a statement:
395
+
364
396
{% arangoshexample examplevar="examplevar" script="script" result="result" %}
365
397
@startDocuBlockInline 06_workWithAQL_statements11
366
398
@EXAMPLE_ARANGOSH_OUTPUT{06_workWithAQL_statements11}
@@ -371,8 +403,10 @@ make the server return that by setting the *profile* attribute to
371
403
@endDocuBlock 06_workWithAQL_statements11
372
404
{% endarangoshexample %}
373
405
{% include arangoshexample.html id=examplevar script=script result=result %}
406
+
374
407
After executing this query, you can use the * getExtra()* method of the cursor to get the
375
408
produced statistics:
409
+
376
410
{% arangoshexample examplevar="examplevar" script="script" result="result" %}
377
411
@startDocuBlockInline 06_workWithAQL_statements12
378
412
@EXAMPLE_ARANGOSH_OUTPUT{06_workWithAQL_statements12}
@@ -383,11 +417,13 @@ produced statistics:
383
417
@endDocuBlock 06_workWithAQL_statements12
384
418
{% endarangoshexample %}
385
419
{% include arangoshexample.html id=examplevar script=script result=result %}
420
+
386
421
Query validation
387
422
----------------
388
423
389
424
The * _ parse* method of the * db* object can be used to parse and validate a
390
425
query syntactically, without actually executing it.
426
+
391
427
{% arangoshexample examplevar="examplevar" script="script" result="result" %}
392
428
@startDocuBlockInline 06_workWithAQL_statements13
393
429
@EXAMPLE_ARANGOSH_OUTPUT{06_workWithAQL_statements13}
0 commit comments