ArangoDB:与嵌套对象集合键联接

wi3ka0sx  于 2022-12-09  发布在  Go
关注(0)|答案(2)|浏览(145)

需要一些关于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 })

它将groups作为Array返回,但需要将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" }]
  }
}

您的帮助将不胜感激!

vsaztqbk

vsaztqbk1#

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

xqkwcwgp2#

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

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

相关问题