Home > Software engineering >  find fields with average greater than x and group by other field mongo db
find fields with average greater than x and group by other field mongo db

Time:05-05

I have a query which find the goals average of football teams, group by teams.

db.matchs.aggregate([{$group: {_id:"$equipes_domicile", pop: {$avg:"$score_equipe_domicile"} } }])

but i want only select them with an average greater than 4.

CodePudding user response:

You can do:

db.matchs.aggregate([
     {$group: {_id:"$equipes_domicile", pop: {$avg:"$score_equipe_domicile"} } },
     {$match: {pop: {$gt: 4}}}
])

The $match will choose only documents with pop greater than 4.

  • Related