【问题标题】:ArangoDB for each distinct push other element to an array每个不同的 ArangoDB 将其他元素推送到数组
【发布时间】:2020-11-19 17:18:26
【问题描述】:

所以我在 ArangoDB 中有一个集合,其中子网被分配给团队,我想重新格式化该数据以使每个团队有一行,其中包含分配的子网数组

集合(Assigment)如下所示:

查询

FOR a IN Assigment
    RETURN a

结果

[
  {
    "assignment_type": "subnet",
    "assigment_crteria": "192.168.1.0/24",
    "team_assignment": "Team1",
    "prime_contact": "John",
  },
  {
    "assignment_type": "subnet",
    "assigment_crteria": "192.168.2.0/24",
    "team_assignment": "Team1",
    "prime_contact": "John",
  },
  {
    "assignment_type": "subnet",
    "assigment_crteria": "192.168.3.0/24",
    "team_assignment": "Team1",
    "prime_contact": "John",
  },
  {
    "assignment_type": "subnet",
    "assigment_crteria": "192.168.4.0/24",
    "team_assignment": "Team2",
    "prime_contact": "Bob",
  },
  {
    "assignment_type": "subnet",
    "assigment_crteria": "192.168.5.0/24",
    "team_assignment": "Team2",
    "prime_contact": "Bob",
  },
  {
    "assignment_type": "subnet",
    "assigment_crteria": "192.168.8.0/24",
    "team_assignment": "Team5",
    "prime_contact": "Anna",
  },
  {
    "assignment_type": "subnet",
    "assigment_crteria": "192.168.9.0/24",
    "team_assignment": "Team2",
    "prime_contact": "Bob",
  },
  {
    "assignment_type": "subnet",
    "assigment_crteria": "192.168.10.0/24",
    "team_assignment": "Team2",
    "prime_contact": "Bob",
  },
  {
    "assignment_type": "subnet",
    "assigment_crteria": "192.168.12.0/24",
    "team_assignment": "Team5",
    "prime_contact": "Anna",
  },
  {
    "assignment_type": "subnet",
    "assigment_crteria": "192.168.13.0/24",
    "team_assignment": "Team1",
    "prime_contact": "John",
  },
  {
    "assignment_type": "subnet",
    "assigment_crteria": "192.168.15.0/24",
    "team_assignment": "Team1",
    "prime_contact": "John",
  },
  {
    "assignment_type": "subnet",
    "assigment_crteria": "192.168.17.0/24",
    "team_assignment": "Team7",
    "prime_contact": "Erick",
  }
]

我想用这种格式将重新格式化的文档插入到不同的集合(Team_to_Subnet)中:

结果

[
  {
    "team": "Team1",
    "cidrs": ["192.168.1.0/24","192.168.2.0/24","192.168.3.0/24","192.168.13.0/24","192.168.15.0/24"],
    "prime_contact": "John"
  },
  {
    "team": "Team2",
    "cidrs": ["192.168.4.0/24","192.168.5.0/24","192.168.9.0/24","192.168.10.0/24"],
    "prime_contact": "Bob"
  },
  {
    "team": "Team5",
    "cidrs": ["192.168.8.0/24","192.168.12.0/24"],
    "prime_contact": "Anna"
  },
  {
    "team": "Team7",
    "cidrs": ["192.168.17.0/24"],
    "prime_contact": "Erick"
  },
]

我该怎么做呢?甚至有可能做到吗?我已经研究过使用 COLLECT,但我就是想不通。

任何帮助将不胜感激

【问题讨论】:

    标签: arangodb


    【解决方案1】:

    您可以按团队分组并与COLLECT 联系,并使用projection expression 获取属于相应组的子集数组:

    FOR a IN Assigment
      COLLECT team = a.team_assignment, prime_contact = a.prime_contact
      INTO cidrs = a.assigment_crteria
      RETURN { team, prime_contact, cidrs }
    

    要保存结果,只需将最后一行替换为:

      INSERT { team, prime_contact, cidrs } INTO Team_to_Subnet
    

    【讨论】:

    • 非常感谢,这正是我所需要的。非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2013-05-28
    • 2013-01-21
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多