'find fields with average greater than x and group by other field mongo db

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.



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 nimrod serok