// Bhylu MongoDB 8.0.12 lab: $unwind // Run only against your separate authenticated loopback learning instance. async function main() { if (process.env.BHYLU_MONGO_LAB !== "YES") throw new Error("Set BHYLU_MONGO_LAB=YES for a private lab"); const raw = process.env.BHYLU_MONGO_URI; if (!raw) throw new Error("Set BHYLU_MONGO_URI privately; do not commit credentials"); const address = new URL(raw); if ( address.protocol !== "mongodb:" || address.hostname !== "127.0.0.1" || !address.port || address.port === "27017" ) throw new Error("Use a private loopback MongoDB on a non-default port"); const marker = new ObjectId().toString(), dbName = "bhylu_mongo_" + marker; const connection = new Mongo(raw); const db = connection.getDB(dbName); let owned = false; const emit = (value) => print(JSON.stringify(value)); try { if ((await db.getCollectionNames()).length) throw new Error("Refusing an existing database"); await db.createCollection("_bhylu_owner"); await db.getCollection("_bhylu_owner").insertOne({ _id: marker }); owned = true; await db.getCollection("kits").insertMany([ { _id: "K1", label: "Birdwatch pack", category: "observation", available: 3, condition: "ready", location: { room: "North", shelf: 2 }, tags: ["outdoor", "compact"], notes: null, checks: [ { kind: "lens", passed: true }, { kind: "strap", passed: false }, ], }, { _id: "K2", label: "Stream sampler", category: "sampling", available: 1, condition: "ready", location: { room: "South", shelf: 1 }, tags: ["outdoor", "washable"], checks: [ { kind: "lens", passed: false }, { kind: "strap", passed: true }, ], }, { _id: "K3", label: "Soil survey set", category: "sampling", available: 0, condition: "repair", location: { room: "North", shelf: 3 }, tags: ["washable"], notes: "Replace pouch", checks: [], }, { _id: "K4", label: "Weather logger", category: "observation", available: 2, condition: "ready", location: { room: "South", shelf: 2 }, tags: [], notes: null, checks: [], }, ]); await db.getCollection("members").insertMany([ { _id: "M1", name: "Asha", area: "North" }, { _id: "M2", name: "Dev", area: "South" }, { _id: "M3", name: "Noor", area: "West" }, ]); await db.getCollection("loans").insertMany([ { _id: "L1", kitId: "K1", memberId: "M1", quantity: 1, status: "returned", openedAt: new Date("2026-01-10T09:00:00Z"), }, { _id: "L2", kitId: "K1", memberId: "M2", quantity: 2, status: "open", openedAt: new Date("2026-01-11T09:00:00Z"), }, { _id: "L3", kitId: "K2", memberId: "M1", quantity: 1, status: "open", openedAt: new Date("2026-01-11T10:00:00Z"), }, { _id: "L4", kitId: "K3", memberId: "M2", quantity: 1, status: "returned", openedAt: new Date("2026-01-10T11:00:00Z"), }, { _id: "L5", kitId: "K4", memberId: null, quantity: 1, status: "planned", openedAt: new Date("2026-01-12T09:00:00Z"), }, ]); const kits = db.getCollection("kits"), loans = db.getCollection("loans"), members = db.getCollection("members"); // Lesson commands begin here. emit( await kits .aggregate([ { $unwind: "$tags" }, { $group: { _id: "$tags", kits: { $sum: 1 } } }, { $sort: { _id: 1 } }, ]) .toArray(), ); } finally { if (owned) { const owner = await db .getCollection("_bhylu_owner") .findOne({ _id: marker }); if (!owner || !/^bhylu_mongo_[a-f0-9]{24}$/.test(dbName)) throw new Error("Refusing cleanup without matching ownership marker"); await db.dropDatabase(); } // The noninteractive mongosh process closes its connection when it exits. } } main().catch((error) => { console.error(error.message); exit(1); });