MongoDB compass simple group by query

Viewed 9135

How can I run such an SQL query in MongoDB Compass?

select subject,count(*) from books group by subject
1 Answers

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}
    }
}])
Related