December 7, 2026 · Advent of MongoDB
7. The night shift Solved
Medium$hour$dateToString
The Nice List Office is open 8:00 to 19:00. Nobody should be writing to the list in the middle of the night. All times in the data are North Pole time.
Your task
Find the entries made between 00:00 and 04:59, and count them per night. Return night (the date, as a "YYYY-MM-DD" string) and entries, in date order.
Your answer returns 6 documents with the fields night, entries. Order matters.
What happens next
One lonely entry early in the month, and then a burst of night-time edits across several nights in late November. Someone has been very busy while everyone slept.
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
$hour gives the hour; it needs $expr inside $match. $dateToString with format: "%Y-%m-%d" gives the night.Solution
db.niceListLog.aggregate([
{ $match: { $expr: { $lt: [{ $hour: "$loggedAt" }, 5] } } },
{ $group: { _id: { $dateToString: { format: "%Y-%m-%d", date: "$loggedAt" } }, entries: { $sum: 1 } } },
{ $project: { _id: 0, night: "$_id", entries: 1 } },
{ $sort: { night: 1 } }
])
Free to play. Please don't republish the puzzles or solutions. See the Playground License Terms. Also on Advent of SQL.