在单个 AQL 查询中插入和 return 文档

insert and return document in a single AQL query

当我使用 AQL 将文档插入集合时,它 returns 是一个空列表。

arangosh [test]> db._query('INSERT @document INTO vertices', {document: {name: "bar"}}).toArray()
[ ]

有没有办法在单个 AQL 查询中插入文档并取回完整的文档?

我希望得到的是:

{
  "_id": "vertices/641272433780",
  "_key": "641272433780",
  "_rev": "641272433780",
  "name": "bar"
}

功能实施后,它看起来像这样:

INSERT expression IN|INTO collection [ OPTIONS expression ] WITH NEW INTO variable RETURN variable;

REMOVE expression IN|INTO collection [ OPTIONS expression ] WITH OLD INTO variable RETURN variable;

UPDATE expression IN|INTO collection [ OPTIONS expression ] WITH OLD|NEW INTO variable RETURN variable
UPDATE expression WITH expression IN|INTO collection [ OPTIONS expression ] WITH OLD|NEW INTO variable RETURN variable

`

自 ArangoDB 2.4 以来这是可能的。到return创建的文档,原始查询

INSERT @document INTO vertices

必须改成

INSERT @document INTO vertices LET result = NEW RETURN result

这也适用于多文档 INSERT 以及 UPDATE/REPLACE 和 REMOVE。 2.4 documentation 中的以下引述描述了语法:

To return documents from a data-modification query, the INSERT, REMOVE, UPDATE or REPLACE statement must be immediately followed by a LET statement that assigns either the pseudo-value NEW or OLD to a user-defined variable. The LET statement must be followed by a RETURN statement that returns the variable introduced by LET.

NEW refers to the inserted or modified document revision, and OLD refers to the document revision before update or removal. INSERT statements can only refer to the NEW pseudo-value, and REMOVE operations only to OLD. UPDATE and REPLACE can refer to either.