MongoDB $unwind and array pipelines
Flattening arrays so you can group across their elements. Β· 8 practice questions Β· Cheat sheet β
$unwind turns one document with an array of n elements into n documents, each holding a single element in place of the array. That is the step that makes array contents groupable: unwind grades, then $group on "$grades.grade" to count A grades per cuisine. A $unwind example in full form, { $unwind: { path: "$grades", preserveNullAndEmptyArrays: true } }, keeps documents whose array is missing or empty, which the short form silently drops.
The pipelines here follow the classic shape: $match to narrow, $unwind to flatten, $group to aggregate, $sort to rank. $sortByCount collapses the group-and-sort pair into one stage. Watch document counts as you go: unwinding a large array on a large collection multiplies the work, so match first and project away fields you will not need.
MongoDB $unwind practice questions
- Flatten the
gradesarray and count how many grades of each letter (A,B,C) exist across all restaurants. Output the letter as_idand the count ascount. easy restaurants - Count how many
Agrades each cuisine has earned, asaGrades, sorted descending then by cuisine ascending. medium restaurants - Across all orders, compute the total quantity sold per
skuasunitsSold, sorted by sku ascending. medium orders - Which genres appear most often? Flatten
genresand use$sortByCount. medium movies - How many inspections happened in each calendar month? Unwind
gradesand group by the month number ofgrades.dateas_id, count asinspections, sorted by month. hard restaurants - Unwind
gradesforBombay Spice Houseand include the array position as a field calledgradeIdx. Outputname,gradeIdxandgrades.gradeonly, no_id. hard restaurants - Which 3 actors appear in the most movies? Unwind
cast, count per actor asfilms, sort descending then by name ascending, and take the top 3. hard movies - Find the average grade score per
boroughasavgScore, sorted ascending (best inspection scores first β lower is better). hard restaurants
Each question is answered in a live mongo-shell editor on this page; the result is checked against a reference solution.
$unwind and $sortByCount cheat sheet
One document per array element
{ $unwind: "$grades" }
A restaurant with 3 grades becomes 3 documents, each with grades as a single
object instead of an array. Everything else is copied.
The full form
{ $unwind: {
path: "$grades",
includeArrayIndex: "gradeIdx", // adds 0,1,2...
preserveNullAndEmptyArrays: true // keep docs with [] or missing
} }
By default, documents with an empty or missing array are dropped.
That silently loses rows β reach for preserveNullAndEmptyArrays when counting.
The classic combo
db.restaurants.aggregate([
{ $unwind: "$grades" },
{ $match: { "grades.grade": "A" } },
{ $group: { _id: "$cuisine", aGrades: { $sum: 1 } } },
{ $sort: { aGrades: -1 } }
])
unwind β match β group β sort. Most "count things inside arrays" questions are
this exact shape.
$sortByCount is unwind's best friend
{ $unwind: "$genres" },
{ $sortByCount: "$genres" } // == $group _id + $sum:1, then $sort desc
Gotchas
$matchbefore$unwindfilters documents; after$unwindit
filters individual elements. Both are useful β know which you meant.
- Unwinding two arrays multiplies rows (a cartesian product). Rarely intended.
Common questions
What does $unwind do in MongoDB?
grades becomes three documents, each with grades set to a single grade object. Everything else is copied. After that you can $match, $group or $sort on the element's fields.Why do documents disappear after $unwind?
$unwind drops documents where the array is missing, null or empty. Pass the full form with preserveNullAndEmptyArrays: true to keep them, with the field absent or null in the output document.How do I count values inside arrays across a collection?
[{ $unwind: "$grades" }, { $group: { _id: "$grades.grade", n: { $sum: 1 } } }]. { $sortByCount: "$grades.grade" } after the unwind does the count and the descending sort in one stage.Continue:β Grouping & AccumulatorsJoins & Advanced Stages β
MongoDB topics:Finding & ProjectionComparison OperatorsLogical OperatorsElement & TypePattern MatchingSort, Limit & SkipArray QueriesEmbedded DocumentsAggregation BasicsGrouping & AccumulatorsJoins & Advanced StagesFull cheat sheet