Medium javascript

Average salary by department

Problem

Aggregation pipeline: group by department, avg salary.

Hints
  • $group with _id: "$department" and $avg

Your practice code

Ready — edit the code above and click Run.

Solution

db.employees.aggregate([
  { $group: { _id: "$department", avgSalary: { $avg: "$salary" } } }
]);

Try solving on your own first, then reveal the official answer.

Explanation

$group stage mirrors SQL GROUP BY—common MongoDB interview task.

Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details