How can i receive a last inserted _key in ArangoDB with AQL query? I put the item in the collection, the following element must contain _key created element. How do I get this _key?
-
Are you referring to an AQL INSERT query that should return the generated key(s)? Or to an AQL select query that is executed after a previous insert operation (in a separate query or operation) has taken place? – stj Oct 10 '14 at 13:03
-
@k.djonua: Do you still need help with this problem? – 13abylon Nov 03 '14 at 13:35
-
@13abylon, no, thanks. I'm resolve this trouble with other method. – jonua Nov 03 '14 at 16:04
-
@k.djonua Could you write an answer how you solved the problem for future reference. – 13abylon Nov 03 '14 at 16:24
3 Answers
Update on this question: Since ArangoDB 2.4 it is possible to retrieve the just-inserted document (or documents) even with an AQL query.
With previous versions of ArangoDB 2.3, the syntax for a single document INSERT was:
INSERT { value: 1 } IN collection
with no way to retrieve the system attributes (_key
, _rev
etc.) for the just-inserted document. Since 2.4, the following is possible, too:
INSERT { value: 1 } IN collection LET result = NEW RETURN result
The above returns the created document, including the specified attributes (value
in the above case) and the system attributes.
It also works for multi-document inserts, e.g. the following query
FOR i IN 1..10
INSERT { value: i } IN collection
can be turned into
FOR i IN 1..10
INSERT { value: i } IN collection LET result = NEW RETURN result
to return all inserted documents.

- 9,037
- 19
- 33
Sadly it is not possible at the moment (2.3) to receive the last inserted _key
with an AQL query.
However you can use db.<collection>.save({ Hello : "World" }):
to retrieve the latest _key

- 550
- 2
- 13
-
To clarify: db.
.save returns a document which has the `_key` attribute. – moonglum Dec 16 '14 at 15:21 -
Since 2.4 now it is possible. So if you are using at least ArangoDB 2.4 ignore my answer and look to stj's answer – 13abylon Feb 06 '15 at 14:09
return document
INSERT { myValue: "123", myValue2: "456" } INTO myCollection RETURN NEW
return _key only
INSERT { myValue: "123", myValue2: "456" } INTO myCollection LET inserted = NEW RETURN inserted._key

- 5,766
- 1
- 20
- 16