How can I run such an SQL query in MongoDB Compass?
select subject,count(*) from books group by subject
How can I run such an SQL query in MongoDB Compass?
select subject,count(*) from books group by subject
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}
}
}])