简体   繁体   English

如何将 AQL 的所有结果合并到具有自定义属性的单个文档中

[英]How to merge all results of AQL into single document with custom properties

I have an AQL query traversing graph that always should return a fixed amount of documents from a unique set of collections.我有一个 AQL 查询遍历图,它总是应该从一组唯一的集合中返回固定数量的文档。 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为了从文档中获取集合名称,请使用PARSE_IDENTIFIER 函数,该函数分别给出文档的集合名称和键
  • 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}
)

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

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