1
$\begingroup$

Im Working With Mongodb And Wanted to do a query using Aggregate fucntion. Query Is Each city has several zip codes. Find the city in each state with the most number of zip codes and rank those cities along with the states using the city populations.

The documents are in the following format

{
  "_id": "10280",
  "city": "NEW YORK",
  "state": "NY",
  "pop": 5574,
  "loc": [
    -74.016323,
    40.710537
  ]
}

I was able to count no of Zipcodes for each state using below command

db.zips.aggregate([{"$group":{_id:"$state",count:{$sum:1}}}])

Zips is name of collection

What should be my approach to solve the query?

$\endgroup$