December 9, 2026 · Advent of MongoDB
9. Wish you were here Solved
Medium$lookup$anyElementTrue$map
"They were at the Gingerbread Spa," Tansy says. "Booked months ago." Time off is recorded on each elf, in a leave array of { from, to } dates (both days included; to is midnight at the start of the last day off).
Your task
Count the Nice List entries (at any time of day) that were recorded under the badge of an elf who was on leave that day. Return elf and entries, most entries first, then by name.
Your answer returns 3 documents with the fields elf, entries. Order matters.
What happens next
Every one of those night entries was made while its badge owner was soaking in a gingerbread hot tub. Somebody borrowed their badges. You write a single word on a sticky note: INSIDER.
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
$loggedAt. The last day off ends at to plus one day ({ $add: ["$$l.to", 86400000] } milliseconds).Common traps
tois the start of the last day: an entry at 02:00 on that day is aftertobut still during the leave.
Solution
db.niceListLog.aggregate([
{ $lookup: { from: "elves", localField: "recordedBy", foreignField: "_id", as: "elf" } },
{ $unwind: "$elf" },
{ $match: { $expr: { $anyElementTrue: [{ $map: {
input: "$elf.leave", as: "l",
in: { $and: [{ $gte: ["$loggedAt", "$$l.from"] }, { $lt: ["$loggedAt", { $add: ["$$l.to", 86400000] }] }] }
} }] } } },
{ $group: { _id: "$elf.name", entries: { $sum: 1 } } },
{ $project: { _id: 0, elf: "$_id", 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.