To group by in MongoDB, you can use the $group
aggregation pipeline operator. The $group
operator groups documents by a specified key or keys and applies an aggregation expression to each group. Let’s look at some examples on how to groupby in mongodb.
Here’s an example of how to use the $group
operator in MongoDB to group documents by a specific field:
db.collection.aggregate([
{
$group: {
_id: { field1: "$field1_to_group_by", field2: "$field2_to_group_by" },
count: { $sum: 1 }
}
}
])
This query will group documents in the “collection” collection by the “field1_to_group_by” and “field2_to_group_by” fields and return a list of documents, with each document containing an _id
field that corresponds to the values of the “field1_to_group_by” and “field2_to_group_by” fields and a count
field that indicates the number of documents in each group.
Note that the syntax and parameters may vary depending on the version of MongoDB and the specific use case. For more information on the $group
operator and other MongoDB Aggregation Pipeline operators, you can refer to the MongoDB documentation.
Let’s take a look at real time example of how to GroupBy in MongoDB
Let’s say you have a collection in your MongoDB database called “sales”, which contains documents that represent sales made by a company. Each document in the “sales” collection has fields such as “date”, “product”, “quantity”, and “revenue”. You want to know the total revenue generated by the company for each product over the entire date range. You can use the $group
operator to group the documents in the “sales” collection by the “product” field, and calculate the sum of the “revenue” field for each group. Here’s an example query:
db.sales.aggregate([
{
$group: {
_id: "$product",
total_revenue: { $sum: "$revenue" }
}
}
])
In this query, the $group
operator is used to group the documents in the “sales” collection by the “product” field. The _id
field specifies the key to group by, which in this case is the “product” field. The total_revenue
field specifies the aggregation expression to apply to each group, which in this case is the $sum
expression that calculates the sum of the “revenue” field for each group.
This query will return a list of documents, with each document containing the _id
field that corresponds to the value of the “product” field and a total_revenue
field that indicates the sum of the “revenue” field for each group.
For example, if the “sales” collection contains the following documents:
{ date: "2022-01-01", product: "A", quantity: 10, revenue: 100 }
{ date: "2022-01-02", product: "B", quantity: 5, revenue: 50 }
{ date: "2022-01-03", product: "A", quantity: 15, revenue: 150 }
{ date: "2022-01-04", product: "C", quantity: 20, revenue: 200 }
{ date: "2022-01-05", product: "B", quantity: 10, revenue: 100 }
The query will return the following result:
{ _id: "A", total_revenue: 250 }
{ _id: "B", total_revenue: 150 }
{ _id: "C", total_revenue: 200 }
This indicates that the company generated a total revenue of 250 for product A, 150 for product B, and 200 for product C over the entire date range.
Hope the example covered here helped you find how to group by in MongoDB.
Also Read: How to Join two collections in MongoDB – TechStack4U