在 COLLECT 中使用 INTO,同时还使用 AGGREGATE。可以吗?

Using INTO in COLLECT while also using AGGREGATE. Can it be done?

我使用的文档的形状类似于:

{
 "survey_id": 123,
  "numerical_answers": [
    {
      "component_id": 345,
      "number": 5,
    },
    {
      "component_id": 346,
      "number": 5,
    }
  ]
}

ArangoDB 的输出应该是这样的:

[
  {
    "component_id": 345,
    "distribution": [
      {
        "component_id": 345,
        "score": null,
        "total": 42
      },
      {
        "component_id": 345,
        "score": 1,
        "total": 76
      },
      {
        "component_id": 345,
        "score": 2,
        "total": 37
      },
      {
        "component_id": 345,
        "score": 3,
        "total": 40
      },
      {
        "component_id": 345,
        "score": 4,
        "total": 93
      },
      {
        "component_id": 345,
        "score": 5,
        "total": 212
      }
    ],
    "total": 500,
    "avg": 3.404
  }
]

我的 AQL 如下所示:

FOR doc IN @@collection
  FILTER doc.`survey_id` == @survey_id

  LET componentScoreGroup = {
    component_id: @component_id,
    score: FIRST(doc.numerical_answers[* filter CURRENT.component_id == @component_id return CURRENT.number])
  }

  COLLECT component_id = componentScoreGroup.component_id, score = componentScoreGroup.score WITH COUNT INTO total

  LET distribution = {
    component_id: component_id,
    score: score,
    total: total
  }

  COLLECT component_id2 = distribution.component_id into groups keep distribution

  LET finalTotal = sum(groups[*].distribution.total), 
      summedScore = sum(groups[* return CURRENT.distribution.score * CURRENT.distribution.total])

  RETURN {
    component_id: component_id2,
    distribution: groups[*].distribution,
    total: finalTotal,
    avg: summedScore / finalTotal
  }

我想在 COLLECT 中使用性能更高的 AGGREGATE 语法,但我不确定是否可以,因为我还想在最终 return.

如有任何帮助,我们将不胜感激!

这是否更接近您要查找的内容?

FOR doc IN @@collection
  FILTER doc.survey_id == @survey_id

     LET componentScoreGroup = {
          component_id: @component_id,
          score: FIRST(doc.numerical_answers[* filter CURRENT.component_id == @component_id return CURRENT.number])
        }

    COLLECT id = componentScoreGroup.component_id
    AGGREGATE avg = AVG(componentScoreGroup.score),
              total = SUM(componentScoreGroup.score)
    INTO group
    return { "id" : id , "avg" : avg , "total" : total, "number" : LENGTH(group), "group" : group}