December 21, 2026 · Advent of MongoDB
21. Racing the sun Solved
Medium$lookup$group
Santa delivers at midnight, local time, so the route starts where midnight comes first: the biggest utcOffset, then westward. The plan uses the true list.
Your task
For the children who are nice on the true list (ignoring the fake entries), group by utcOffset. Return utcOffset, children and loadKg (the weight of all their wished toys), from the biggest offset to the smallest.
Your answer returns 13 documents with the fields utcOffset, children, loadKg. Order matters.
What happens next
Auckland first, Los Angeles last. Pepper looks at the total and at the capacity. "It still won't all fit." Grizzle, red-eyed, raises a hand. "What if... we go twice?"
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
Common traps
- Group children first (their wishlist weight per child), then by time zone, so each child counts once.
Solution
db.niceListLog.aggregate([
{ $group: { _id: "$childId", score: { $sum: { $cond: [{ $and: [{ $eq: ["$terminal", "LOGI-07"] }, { $lt: [{ $hour: "$loggedAt" }, 5] }] }, 0, "$points"] } } } },
{ $match: { score: { $gte: 0 } } },
{ $lookup: { from: "children", localField: "_id", foreignField: "_id", as: "c" } },
{ $unwind: "$c" },
{ $lookup: { from: "toys", localField: "c.wishlist", foreignField: "_id", as: "gifts" } },
{ $group: { _id: "$c.utcOffset", children: { $sum: 1 }, grams: { $sum: { $sum: "$gifts.weightG" } } } },
{ $project: { _id: 0, utcOffset: "$_id", children: 1, loadKg: { $divide: ["$grams", 1000] } } },
{ $sort: { utcOffset: -1 } }
])
Free to play. Please don't republish the puzzles or solutions. See the Playground License Terms. Also on Advent of SQL.