问题描述
我正在尝试解决 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}
解决方法
使用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}
我没有针对您的数据集进行测试,而是使用这个小片段:
LET r = {'grades': [ {score: 5},{score: 7},{score: 15} ] }
RETURN MAX(r.grades[*].score)