简体   繁体   English

在单个AQL查询中插入并返回文档

[英]insert and return document in a single AQL query

When I insert a document into a collection with AQL it returns an empty list. 当我使用AQL将文档插入到集合中时,它将返回一个空列表。

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

Is there a way to insert a document and get the complete document back in a single AQL query? 有没有办法在单个AQL查询中插入文档并获取完整文档?

What I am hoping to get back is: 我希望能得到的是:

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

This is possible since ArangoDB 2.4. 从ArangoDB 2.4开始,这是可能的。 To return the created document, the original query 要返回创建的文档,原始查询

INSERT @document INTO vertices

has to be changed into 必须改为

INSERT @document INTO vertices LET result = NEW RETURN result

This will also work for multi-document INSERTs and also for UPDATE/REPLACE and REMOVE. 这也将适用于多文档INSERT,以及UPDATE / REPLACE和REMOVE。 The following quotes from the 2.4 documentation describe the syntax: 2.4文档中的以下引号描述了语法:

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. 要从数据修改查询返回文档,必须在INSERT,REMOVE,UPDATE或REPLACE语句后紧跟一个LET语句,该语句将伪值NEW或OLD分配给用户定义的变量。 The LET statement must be followed by a RETURN statement that returns the variable introduced by LET. LET语句之后必须是RETURN语句,该语句返回LET引入的变量。

NEW refers to the inserted or modified document revision, and OLD refers to the document revision before update or removal. NEW是指插入或修改的文档修订,而OLD是指更新或删除之前的文档修订。 INSERT statements can only refer to the NEW pseudo-value, and REMOVE operations only to OLD. INSERT语句只能引用NEW伪值,而REMOVE操作只能引用OLD。 UPDATE and REPLACE can refer to either. UPDATE和REPLACE都可以引用。

Once the feature is implemented, its going to look like that: 功能实现后,其外观将如下所示:

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

` `

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM