1

在 ArangoDB 连接查询方面需要一些帮助。我有如下文档,其中 User 包含嵌套对象中的 Group 集合键(groups)。当我查询用户时,我需要加入组集合并更改响应。应该WITH QUERY只做。请检查下面给出的示例以获得更清晰的信息。

// User Collection
[
  {
    _key: "312121"
    name: "Ash",
    groups: {
      "1": ["323223", ...etc], // Group collection _keys;
      "2": ["342323", ...etc] // Group collection _keys;
    } 
  }
]

// Group Collection
[
  {
    _key: "323223"
    name: "Group 1"
  },
  {
    _key: "313131"
    name: "Group 2"
  }
]

我试过这个查询:

LET user = DOCUMENT('users/312121')
LET groups_keys = ATTRIBUTES(user.groups) // ["1", "2"]
LET list = (
  FOR key IN groups_keys
  LET groups = (
     FOR group_key IN user.groups[key] // user.groups["1"] and so on..
     LET group = DOCUMENT(CONCAT('groups/', group_key))
     RETURN { group_id: group._key, name: group.name }
  )
  RETURN { [key]: groups }
)

RETURN MERGE(user, { groups: groups })

它返回groupsasArray但需要groups成为Object

// Current Output: 
{
  _key: "312121"
  name: "Ash",
  groups: [ // <-- This should be object, not an array
    {
      "1": [{ _key: "323223", name: "Group 1" }]
    },
    {
      "2": [{ _key: "313131", name: "Group 2" }]
    }
  ]
}

但它应该是这种格式:

// Expected Output:

{
  _key: "312121"
  name: "Ash",
  groups: { // <-- like this
    "1": [{ _key: "323223", name: "Group 1" }],
    "2": [{ _key: "313131", name: "Group 2" }]
  }
}

您的帮助将不胜感激!

4

2 回答 2

1

使用MERGE

MERGE[ docs ] 也接受一个数组参数:

LET list = MERGE(
  /* omitted */
  RETURN { [key]: groups }
)

使用ZIP

替代解决方案也可以使用ZIP[ docs ]:

LET list = (
  /* omitted */
  RETURN { key, groups } // Notice the format
)
LET obj = ZIP(list[*].key, list[*].groups)
于 2020-11-10T08:47:33.850 回答
0

试试这个希望这会帮助你

const newObject = {};
for (const [key, value] of Object.entries(grouped)) { 
  newObject[value[0].<your choice of key or>] = value
}
于 2020-11-09T10:22:23.737 回答