Challenge 3: Design a $bucket Age Histogram — Possible Solution ==================================================================== db.users.aggregate([ { $bucket: { groupBy: "$age", boundaries: [18, 26, 36, 51], default: "50+", output: { count: { $sum: 1 } } } } ]) WHY THIS WORKS AS AN ANSWER ------------------------------ $bucket boundaries are [inclusive, exclusive) — each boundary value belongs to the bucket that STARTS at it, not the one that ends there. So boundaries: [18, 26, 36, 51] creates three buckets: - _id: 18 -> ages 18 up to (not including) 26 -> "18-25" - _id: 26 -> ages 26 up to (not including) 36 -> "26-35" - _id: 36 -> ages 36 up to (not including) 51 -> "36-50" That's why the boundary after 36 is 51, not 50 — a user who is exactly 50 needs to still fall inside the 36-50 bucket, and since 51 is the EXCLUSIVE upper edge, age 50 is included while age 51 is not. default: "50+" catches every age that doesn't fall inside any of those three ranges — in this design, anyone 51 or older (and, as a boundary condition, MongoDB would also reject/misplace ages below the first boundary of 18 into default, so this design assumes no users under 18 exist in the collection). output: { count: { $sum: 1 } } counts how many users landed in each bucket, the same accumulator pattern as $group.