Challenge 3: Write an Upsert — Possible Solution ==================================================================== db.visitCounters.updateOne( { userId: "u42" }, { $inc: { visits: 1 } }, { upsert: true } ) WHY THIS WORKS AS AN ANSWER ------------------------------ This is exactly the pattern the chapter's page-view example demonstrated, applied to a per-user counter instead of a per-page one: - The FIRST time this runs for userId "u42" (no matching document exists yet), the upsert: true option causes MongoDB to CREATE a new document instead of doing nothing. Because $inc on a non-existent field treats the starting value as 0, the created document ends up as { userId: "u42", visits: 1 } — exactly what the challenge asked for. - EVERY subsequent time this runs for the same userId, a matching document already exists, so $inc simply increments its existing visits field by 1 instead of creating anything new. The key insight being tested: upsert: true doesn't require writing separate "insert if missing" and "update if present" logic yourself — the single updateOne call with upsert: true handles both cases automatically, based purely on whether the filter finds a match.