如何查找平均分数大于5的文档。集合如下所示:
集合餐厅:
{"grades": [{"grade": "A", "score": 2}, {"grade": "A", "score": 6}], "name": "Morris Park Bake Shop", "restaurant_id": "30075445"}
{"grades": [{"grade": "A", "score": 8}, {"grade": "B", "score": 23}], "name": "Wendy'S", "restaurant_id": "30112340"}
{"grades": [{"grade": "A", "score": 2}, {"grade": "A", "score": 11}], "name": "Dj Reynolds Pub And Restaurant", "restaurant_id": "30191841"}
我试过了
db.restaurants.find({average_socre: {$gt: 5}}, {average_socre:{$avg: "$grades.score"}})
但这不起作用。
使用此查询:
db.collection.find({
$expr: {
$gt: [
{
"$avg": "$grades.score"
},
5
]
}
},
{
average_score: {
$avg: "$grades.score"
}
})
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句