December 8, 2026 · Advent of MongoDB
8. Whose badge? Solved
Medium$lookup$group
Every entry records who made it: recordedBy is the _id of the elf whose badge was used to log in.
Your task
For the night entries (00:00 to 04:59), count the entries per elf. Return elf (the elf's name) and entries, most entries first, then by name.
Your answer returns 4 documents with the fields elf, entries. Order matters.
What happens next
Three Nice List clerks, Holly, Juniper and Sprig, share almost all of it between them, plus one entry from Kip in IT. Tansy shakes her head. "Holly, Juniper and Sprig? They're the most careful elves I have. And they weren't even here."
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
Solution
db.niceListLog.aggregate([
{ $match: { $expr: { $lt: [{ $hour: "$loggedAt" }, 5] } } },
{ $group: { _id: "$recordedBy", entries: { $sum: 1 } } },
{ $lookup: { from: "elves", localField: "_id", foreignField: "_id", as: "elf" } },
{ $project: { _id: 0, elf: { $first: "$elf.name" }, entries: 1 } },
{ $sort: { entries: -1, elf: 1 } }
])
Free to play. Please don't republish the puzzles or solutions. See the Playground License Terms. Also on Advent of SQL.