December 16, 2026 · Advent of MongoDB
16. Who was there? Solved
Medium$addToSet$size
Bram Lanternlight runs Security, and every door at the North Pole has a badge reader. doorSwipes records each swipe: the elf, the time, the door, and the direction ('in' or 'out').
Your task
Which elves swiped in to the Logistics Bay between 00:00 and 04:59 in November, and on how many different nights? Return elf and nights, most nights first, then by name.
Your answer returns 3 documents with the fields elf, nights. Order matters.
What happens next
Flurry Icicle, the night guard, is in the bay every night: that's the job. Maple from Logistics once, for a late inventory. And one more name, on just a handful of nights.
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.doorSwipes.aggregate([
{ $match: { door: "Logistics Bay", direction: "in", $expr: { $lt: [{ $hour: "$swipedAt" }, 5] } } },
{ $group: { _id: "$elfId", nights: { $addToSet: { $dateToString: { format: "%Y-%m-%d", date: "$swipedAt" } } } } },
{ $lookup: { from: "elves", localField: "_id", foreignField: "_id", as: "e" } },
{ $project: { _id: 0, elf: { $first: "$e.name" }, nights: { $size: "$nights" } } },
{ $sort: { nights: -1, elf: 1 } }
])
Free to play. Please don't republish the puzzles or solutions. See the Playground License Terms. Also on Advent of SQL.