MongoDB — Grouping & Deconstruction Stages ($group & $unwind)
Group records with $group, calculate metrics ($sum, $avg, $min, $max), and deconstruct arrays using $unwind.
1Deconstructing Arrays with $unwind
$unwind & $group Combination
db.posts.aggregate([
// Flattens array: outputs 1 document per tag
{ $unwind: "$tags" },
// Group by tag and count frequency
{ $group: { _id: "$tags", count: { $sum: 1 } } },
{ $sort: { count: -1 } }
]);