Aggregation pipeline: group by department, avg salary.
Ready — edit the code above and click Run.
db.employees.aggregate([
{ $group: { _id: "$department", avgSalary: { $avg: "$salary" } } }
]);
Try solving on your own first, then reveal the official answer.
$group stage mirrors SQL GROUP BY—common MongoDB interview task.