Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Last inserted _key in ArangoDB with AQL?

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?

like image 633
jonua Avatar asked Dec 19 '22 10:12

jonua


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

like image 195
stj Avatar answered Jan 28 '23 20:01

stj


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

like image 26
13abylon Avatar answered Jan 28 '23 21:01

13abylon