db.users.aggregate([{$group: {_id: '$city', n: {$sum: 1}}}])

Category: Databases & SQL

db.users.aggregate([{$group: {_id: '$city', n: {$sum: 1}}}])

Group documents and count per group

The aggregation pipeline's $group stage buckets documents by city ($city references the field) and $sum: 1 counts each bucket. This is MongoDB's GROUP BY. Related: find() for plain queries.
Looking for more? Search all 7,657 commands — works offline, in English or Spanish, and fixes typos.