【问题标题】:Filter within array in arangodb not working在 arangodb 中的数组内过滤不起作用
【发布时间】:2021-04-19 00:50:00
【问题描述】:

我正在尝试解决this page 上的 Q.20。数据集为here

Q20.编写一个 MongoDB 查询来查找得分不超过 10 的餐厅的餐厅 ID、名称、行政区和美食。

我正在尝试以下方式,但我可以看到我得到的餐馆的结果是 [2,6,10,9,14] 和 [8,23,12,12]。所以我知道这不可能。

for r in restaraunts    
    for g in r.grades
        filter g.score <=10
        return distinct {ID:r.restaurant_id,name:r.name,borough:r.borough,cuisine:r.cuisine,score:r.grades[*].score}
    

请帮忙,我尝试了不同的分数预测,但我觉得我忽略了一些东西。如果我使用 collect 我仍然会出错

    collect rest=r.restaurant_id,name= r.name, bor=r.borough,cuis=r.cuisine, sc=r.grades[*].score into groups
    for g in sc
    filter g<=10
    return {rest:rest,name:name,bor:bor,cuis:cuis,sc:g}

【问题讨论】:

    标签: arangodb aql


    【解决方案1】:

    使用MAX()函数确定要过滤的值:

    for r in restaurants    
        LET score = MAX(r.grades[*].score)
        filter score <=10
        return distinct {ID:r.restaurant_id,name:r.name,borough:r.borough,cuisine:r.cuisine,score:score}
    

    我没有针对您的数据集进行测试,但使用了这个小 sn-p:

    LET r = {'grades': [ {score: 5}, {score: 7}, {score: 15} ] }
    RETURN MAX(r.grades[*].score)
    

    【讨论】:

    • 谢谢!那真的很有帮助。如此精致高效的解决方案!
    猜你喜欢
    • 1970-01-01
    • 2021-03-21
    • 1970-01-01
    • 1970-01-01
    • 2021-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-19
    相关资源
    最近更新 更多