December 13, 2026 · Advent of MongoDB
13. Chain of command Solved
Medium$graphLookupdepthField
Whoever used Holly's badge knew she'd be away. Leave requests go up the chain of management, so you start there. Each elf has a managerId pointing at their manager.
Your task
List everyone above Holly Frostwhistle in the chain of command. Return name and level (1 = her direct manager, 2 = their manager, and so on), in level order.
Your answer returns 3 documents with the fields name, level. Order matters.
What happens next
Clove, Tansy, Pepper. Any of them could have seen the spa booking. But so could anyone who walked past the leave board in the canteen. Dead end? Not quite: every login happens somewhere.
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
$graphLookup from elves, starting with $managerId, connecting managerId to _id. depthField numbers the steps from 0.Solution
db.elves.aggregate([
{ $match: { name: "Holly Frostwhistle" } },
{ $graphLookup: { from: "elves", startWith: "$managerId", connectFromField: "managerId", connectToField: "_id", as: "chain", depthField: "depth" } },
{ $unwind: "$chain" },
{ $project: { _id: 0, name: "$chain.name", level: { $add: ["$chain.depth", 1] } } },
{ $sort: { level: 1 } }
])
Free to play. Please don't republish the puzzles or solutions. See the Playground License Terms. Also on Advent of SQL.