December 14, 2026 · Advent of MongoDB
14. Which terminal? Solved
Medium$lookupstring keys
Kip Circuitbell from IT pushes their glasses up. "Badges can be borrowed. Terminals can't walk." Each entry records the terminal it was typed on, and terminals says where each one is.
Your task
For the night entries (00:00 to 04:59), count entries per terminal. Return terminal, location and entries, most entries first, then by terminal.
Your answer returns 2 documents with the fields terminal, location, entries. Order matters.
What happens next
"IT-01 is me, testing the backup at 2 a.m., sorry," says Kip. Everything else came from LOGI-07, in the Logistics Bay. Not the Nice List Office at all. From now on, the "fake entries" are the night entries on LOGI-07.
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
_ids of terminals: look them up.Solution
db.niceListLog.aggregate([
{ $match: { $expr: { $lt: [{ $hour: "$loggedAt" }, 5] } } },
{ $group: { _id: "$terminal", entries: { $sum: 1 } } },
{ $lookup: { from: "terminals", localField: "_id", foreignField: "_id", as: "t" } },
{ $project: { _id: 0, terminal: "$_id", location: { $first: "$t.location" }, entries: 1 } },
{ $sort: { entries: -1, terminal: 1 } }
])
Free to play. Please don't republish the puzzles or solutions. See the Playground License Terms. Also on Advent of SQL.