Reputation: 19144
How can I run such an SQL query in MongoDB Compass?
select subject,count(*) from books group by subject
Upvotes: 4
Views: 17463
Reputation: 9268
you can use Mongodb Aggregation pipeline with $group with $sum to achieve this.
Try :
db.collection_name.aggregate([{
$group : {
_id : "$subject",
subject : {$first : "$subject"},
count : {$sum : 1}
}
}])
Upvotes: 5