Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading