December 2, 2026 · Advent of MongoDB
2. Letters from everywhere Solved
Easy$group$sort
The mail room is knee-deep in letters. Every child who wrote to Santa this year has a record in children, and the sleigh team wants to know where they all live before they plan the route.
Your task
Count the children per country. Return country and children, the most children first, then by country name.
Your answer returns 15 documents with the fields country, children. Order matters.
What happens next
The sleigh team pins a map next to your list. You notice the USA shows up twice in the address book, once for Chicago and once for Los Angeles. You'll want that later.
Solve today's puzzle to read what happens next.
The data
The North Pole's collections, a snapshot taken on December 1, 2026. Times are North Pole time (UTC). A MongoDB-compatible database runs in your browser: use Explore data to browse it.
elves_id, name, department, hiredOn, managerId (→ elves._id, null for the Head Elf), leave: [{ from, to }]children_id, name, country, city, utcOffset, age, wishlist: [toy _id, …] (top wish first)toys_id, name, category, buildMinutes, weightGniceListLog_id, childId, loggedAt, points, reason, recordedBy (→ elves._id), terminal (→ terminals._id)terminals_id (e.g. "LOGI-07"), location, departmentreindeer_id, name, maxLoadKgflights_id, reindeerId, flownOn, km, carrotsbuilds_id, toyId, elfId, builtAt, minutes, passedQccookieBatches_id, bakedAt, elfId, cookiesdoorSwipes_id, elfId, swipedAt, door, direction ("in" or "out")
Hints
$group on $country, then $project the _id back to country and $sort on both keys.Solution
db.children.aggregate([
{ $group: { _id: "$country", children: { $sum: 1 } } },
{ $project: { _id: 0, country: "$_id", children: 1 } },
{ $sort: { children: -1, country: 1 } }
])
Free to play. Please don't republish the puzzles or solutions. See the Playground License Terms. Also on Advent of SQL.