AVEbrahimi
AVEbrahimi

Reputation: 19144

MongoDB compass simple group by query

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

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

Upvotes: 4

Views: 17463

Answers (1)

Ravi Shankar Bharti
Ravi Shankar Bharti

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

Related Questions