MongoDb Country State City

San Source

How to get the State and City Count for each Country.

db.getCollection('mydatabase.reviews').aggregate( [
        { $group: { _id: { Country: "$Country", State: "$State", City: "$City", z:"_id"}, total: { $sum: 1 } } },
        { $group: {  _id: { Country: "$Country", State:"$_id.State", City:"$_id.City"}, count: {$sum: "$total"}} }  , 
        {$sort: {count: -1} }
 ] )

I want an output like:

Please click to view Table Image

Can anyone please help.

mongodb

Answers

comments powered by Disqus