简体   繁体   English

mongodb:查询平均值大于某个数的文档

[英]mongodb: Query documents with average greater than a number

How do I find the documents with with average score greater than 5. The collection looks like this:如何找到平均分数大于 5 的文档。集合如下所示:
Collection restaurants:集合餐厅:

{"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"}

I tried我试过

db.restaurants.find({average_socre: {$gt: 5}}, {average_socre:{$avg: "$grades.score"}})

but it doesn't work.但它不起作用。

Using this query:使用此查询:

db.collection.find({
  $expr: {
    $gt: [
      {
        "$avg": "$grades.score"
      },
      5
    ]
  }
},
{
  average_score: {
    $avg: "$grades.score"
  }
})

Live居住

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM