ArangoDB分组和排序

xesrikrc  于 2022-12-09  发布在  Go
关注(0)|答案(1)|浏览(207)

在ArangoDB中,我想对通知数据进行分组和排序。
我有以下通知数据集

[
  {id: 1, groupId: 1, text: 'Aoo', time: 23},
  {id: 2, groupId: 2, text: 'Boo', time: 32},
  {id: 3, groupId: 1, text: 'Coo', time: 45},
  {id: 4, groupId: 3, text: 'Doo', time: 56},
  {id: 5, groupId: 1, text: 'Eoo', time: 22},
  {id: 6, groupId: 2, text: 'Foo', time: 23}
]

我想按groupId对通知进行分组,最近的通知组应该显示在顶部。

[
  { groupId: 3, notifications: [{id: 4, groupId: 3, text: 'Doo', time: 56}],
  { groupId: 1, notification: [{id: 3, groupId: 1, text: 'Coo', time: 45}, {id: 1, groupId: 1, text: 'Aoo', time: 23}, {id: 5, groupId: 1, text: 'Eoo', time: 22}]},
  { groupId: 2, notifications: [{id: 2, groupId: 2, text: 'Boo', time: 32}, {id: 6, groupId: 2, text: 'Foo', time: 23}] }
]

尝试遵循AQL

FOR doc IN notificaion
SORT doc.time DESC
COLLECT groupId = doc.groupId INTO g
RETURN { groupId, notifications: g[*].doc }

上述查询对内部组元素进行排序,但不对外部组进行排序。
我正在努力为它构造一个AQL。任何指针都将是有帮助的。
谢谢

ve7v8dk2

ve7v8dk21#

排序两次:一旦收集的文档集-如你已经做的,然后集合:

FOR doc IN notification
  SORT doc.time DESC
  COLLECT groupId = doc.groupId INTO g
  SORT g[*].doc.time DESC
  RETURN { groupId, notifications: g[*].doc }

在我的测试中,这将产生所需的序列:

[
  {
    "groupId": 3,
    "notifications": [
      {
        "id": 4,
        "groupId": 3,
        "text": "Doo",
        "time": 56
      }
    ]
  },
  {
    "groupId": 1,
    "notifications": [
      {
        "id": 3,
        "groupId": 1,
        "text": "Coo",
        "time": 45
      },
      {
        "id": 1,
        "groupId": 1,
        "text": "Aoo",
        "time": 23
      },
      {
        "id": 5,
        "groupId": 1,
        "text": "Eoo",
        "time": 22
      }
    ]
  },
  {
    "groupId": 2,
    "notifications": [
      {
        "id": 2,
        "groupId": 2,
        "text": "Boo",
        "time": 32
      },
      {
        "id": 6,
        "groupId": 2,
        "text": "Foo",
        "time": 23
      }
    ]
  }
]

相关问题