Structuremongokitten 7.9.8MongoKitten
Group
Groups documents by a specified key and can perform aggregations.
struct Group
The Group
stage is used to group documents together and perform calculations across each group.
Examples
// Group by category and count items
Group([
"_id": "$category",
"count": ["$sum": 1]
])
// Group by multiple fields
Group([
"_id": [
"category": "$category",
"supplier": "$supplier"
],
"count": ["$sum": 1],
"avgPrice": ["$avg": "$price"]
])
// Calculate statistics
Group([
"_id": "$department",
"totalSalary": ["$sum": "$salary"],
"avgSalary": ["$avg": "$salary"],
"minSalary": ["$min": "$salary"],
"maxSalary": ["$max": "$salary"],
"employeeCount": ["$sum": 1]
])
Common Aggregation Operators
$sum
: Calculates sum$avg
: Calculates average$min
: Finds minimum value$max
: Finds maximum value$first
: First value in group$last
: Last value in group$push
: Creates an array of values