如何return批量插入的结果?

How to return the results of a bulk insert?

我如何return新插入文件的数组?我想从 Java.

执行批量插入
INSERT {
  text: @text0,
  createdAt: @createdAt0
} IN messages

LET result0 = { id: NEW._key, text: NEW.text, createdAt: NEW.createdAt }

INSERT {
  text: @text1,
  createdAt: @createdAt1
} IN messages

LET result1 = { id: NEW._key, text: NEW.text, createdAt: NEW.createdAt }

RETURN [result0, result1]

除了定义一个新变量来保存结果之外,是否有更好的方法来收集每次插入的结果?

这个查询应该可以满足您的要求。

FOR d IN [
    {text:@text0, createdAt: @createdAt0}, {text:@text1, createdAt: @createdAt1}
]
INSERT d INTO abc
RETURN {
    id: NEW._id,
    text: d.text,
    createdAt: d.createdAt
}

来自 AQL 的示例响应是:

[
  {
    "id": "abc/21971344",
    "text": "apple",
    "createdAt": 1584107091
  },
  {
    "id": "abc/21971345",
    "text": "banana",
    "createdAt": 1584108473
  }
]

只要最终用户不制作 AQL,就应该没问题,尤其是因为您正在使用参数。

如果您曾经在测试之外使用该产品,我绝对建议您查看 Foxx,因为它为您提供了一个抽象层,(相对)托管在数据库中,这至少可以阻止消费者执行根本没有 AQL 查询,而只是通过 REST 端点进行通信。

PS。此查询格式也适用:

FOR d IN @messages
INSERT d INTO abc
RETURN {
    id: NEW._id,
    text: d.text,
    createdAt: d.createdAt
}

其中 @messages

"messages": [
    {
        "text": "apple",
        "createdAt": 1584107091
    },
    {
        "text": "banana",
        "createdAt": 1584108473
    }
]