首页 文章

ArangoDB - 有关AQL聚合的帮助

提问于
浏览
0

我有一个这样的图表:

// [user] -answer-> [question]

for u in user
    filter u._id in ['user/foo', 'user/bar']
    for v, e in 1 outbound u graph 'qaGraph'
        return keep(e, '_from', '_to', 'chosen')

输出:

[
  {
    "_from": "user/foo",
    "_to": "question/A",
    "chosen": 0
  },
  {
    "_from": "user/foo",
    "_to": "question/B",
    "chosen": 0
  },
  {
    "_from": "user/foo",
    "_to": "question/C",
    "chosen": 1
  },
  {
    "_from": "user/bar",
    "_to": "question/A",
    "chosen": 0
  },
  {
    "_from": "user/bar",
    "_to": "question/C",
    "chosen": 0
  }
]

这意味着foo和bar已经回答了两个共同的问题(A&C),但他们只回答了一个问题(A) .

如何编写AQL以以下格式返回相同的信息?

{
  "questions": 2,
  "match": 1
}

我在这里挣扎但没有成功,所以任何帮助都会受到赞赏 .

谢谢!

  • 编辑:我忘了提及所有问题都是多项选择,只有两种选择:0或1.所以 answer.chosen 代表用户的选择 .

1 回答

  • 0

    我最终得到了这个查询:

    let data = (
        for u in user
            filter u._id in ['user/foo', 'user/bar']
            for v, e in 1 outbound u graph 'qaGraph'
                collect question = e._to into answers = e.chosen
                filter count(answers) == 2 // questions answered by both users
                let match = sum(answers) != 1 ? 1 : 0 // flags same answer by both users
                return {question, match}
    )
    return { 
        questions: count(data), 
        matches: sum(data[*].match) 
    }
    

    但是,我想找到一个比这简单的解决方案 .

相关问题