如何将 AQL 的所有结果合并到具有自定义属性的单个文档中 [英] How to merge all results of AQL into single document with custom properties

查看:21
本文介绍了如何将 AQL 的所有结果合并到具有自定义属性的单个文档中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 AQL 查询遍历图,它总是应该从一组唯一的集合中返回固定数量的文档.所以每个集合只会出现一次,并且只有一个文档.

我希望将它们全部合并到反映文档集合名称的属性下的单个文档中.

查询就这么简单:

FOR v IN ANY vertex/key" edge_collection RETURN v

返回示例结果为:

<预><代码>[{"_key": "123","_id": "foo/123","_rev": "_WYhh0ji---","foo_attribute": "lorem impsum"},{"_key": "456","_id": "bar/456","_rev": "_WYhh2ny---","bar_attribute": "dolor 坐 amet"}]

我希望得到这样的:

<预><代码>[{富":{"_key": "123","_id": "foo/123","_rev": "_WYhh0ji---","foo_attribute": "lorem impsum"},酒吧": {"_key": "456","_id": "日历/栏","_rev": "_WYhh2ny---","bar_attribute": "dolor 坐 amet"}}]

解决方案

  • 为了从文档中获取集合名称,请使用 PARSE_IDENTIFIER 函数 分别给出文档的集合名称和键
  • 使用方括号理解生成动态文档属性
  • 简单地合并查询的结果

示例:

返回合并(FOR v IN ANY顶点/键"edge_collection返回 {[PARSE_IDENTIFIER(v).collection]: v})

I have an AQL query traversing graph that always should return a fixed amount of documents from a unique set of collections. So each collection will occur only once and with one document only.

I wish to merge them all into a single document under properties that reflects document’s collection name.

Query as simple as:

FOR v IN ANY "vertex/key" edge_collection RETURN v

Returns a sample result as:

[
  {
    "_key": "123",
    "_id": "foo/123",
    "_rev": "_WYhh0ji---",
    "foo_attribute": "lorem impsum"
  },
  {
    "_key": "456",
    "_id": "bar/456",
    "_rev": "_WYhh2ny---",
    "bar_attribute": "dolor sit amet"
  }
]

That I wish to get like this:

[
  {
    "foo": {
      "_key": "123",
      "_id": "foo/123",
      "_rev": "_WYhh0ji---",
      "foo_attribute": "lorem impsum"
    },
    "bar": {
      "_key": "456",
      "_id": "calendar/bar",
      "_rev": "_WYhh2ny---",
      "bar_attribute": "dolor sit amet"
    }
  }
]

解决方案

  • In order to get collection name from document use PARSE_IDENTIFIER function that gives document’s collection name and key separately
  • Use square brackets comprehension to generate document property dynamically
  • Simply merge result of the query

Example:

RETURN MERGE(
    FOR v IN ANY "vertex/key" edge_collection
    RETURN {[PARSE_IDENTIFIER(v).collection]: v}
)

这篇关于如何将 AQL 的所有结果合并到具有自定义属性的单个文档中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆