ArangoDB:加入嵌套对象集合键

ArangoDB: Join with nested object collection keys

需要一些有关 ArangoDB 连接查询的帮助。我有如下文档,其中用户在嵌套对象 (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 })

其中 returns groupsArray 但需要 groupsObject

// 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" }]
  }
}

非常感谢您的帮助!

试试这个希望这对你有帮助

const newObject = {};
for (const [key, value] of Object.entries(grouped)) { 
  newObject[value[0].<your choice of key or>] = value
}

使用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)