How to group by in mongodb query?

by filiberto , in category: PHP Databases , a year ago

How to group by in mongodb query?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by raven_corwin , 10 months ago

@filiberto You can use the MongoDB $group aggregation operator to group documents from a collection by a specified expression or id:


1
2
3
4
5
6
7
8
db.yourCollection.aggregate([
    {
        "$group": {
            "_id": { key: "$someValue" }, 
            "total": { "$sum": "$someValue" }
        }
    }
])