Search code examples
arangodbaql

Filter within array in arangodb not working


I am trying to solve Q.20 on this page. The dataset is here

Q20.Write a MongoDB query to find the restaurant Id, name, borough and cuisine for those restaurants which achieved a score which is not more than 10.

I am trying it the following way but I can see that I get results of restaurants with scores of [2,6,10,9,14] and [8,23,12,12]. So I know it can't be right.

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}
    

Please help, I have tried different projections of the scores, but I feel I'm overlooking something. If I use collect I still get it wrong

    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}

Solution

  • Use the MAX() function to determin the value to be filtered:

    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}
    

    I didn't test this against your dataset, but with this small snippet:

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