Lab: Mongosh it's your turn
The Mission
Section titled “The Mission”Listen up, Recruit. Professor Solo needs a secure archive for his technical manuals. The old filing cabinet (a JSON file on a USB stick) fell into a volcano. Don’t ask.
Our mission is to initialize a new section of the Vault and populate it properly. No messy grids. Just pure, flexible documents.
Tier 1: Maintenance (The Basics)
Section titled “Tier 1: Maintenance (The Basics)”Objective: Create the container and make a simple deposit.
- Log in: Connect to your Atlas Cluster via
mongosh. - Initialize: Switch to a new database called
library. - Construct: Create a collection called
books. - Populate: Insert ONE book with the following schema:
title: “The Node2Know Manifesto”author: “Professor Solo”year: 2024genres: [“Education”, “Cyberpunk”, “Biography”]isAvailable: true
“I’m trying to insert a document into MongoDB but mongosh keeps giving me a
syntax error. Here is my command: [PASTE COMMAND]. Can you spot the bracket
mismatch?”
Tier 2: The Custom Job (Intermediate)
Section titled “Tier 2: The Custom Job (Intermediate)”Objective: Bulk import and targeted retrieval.
- Bulk Drop: Use
insertMany()to add 3 more books at once.- Constraint: At least one book must be from a different author.
- Constraint: One book should be missing the
yearfield (to prove the schema is flexible).
- The Retrieve: Write a query to find only the books written by “Professor Solo”.
“How do I filter a MongoDB find() query to only show documents where the
author field equals ‘Professor Solo’?”
Tier 3: The Solo Special (Hard)
Section titled “Tier 3: The Solo Special (Hard)”Objective: Advanced filtering and field projection.
We need a manifest for the auditors, but they don’t need to see the _id or the genres. They just want the titles.
- The Audit Query: Find ALL books, but only return the
titleandyearfields. Hide the_id. - The Logic Check: Explain why the document you inserted in Tier 2 (the one without a year) doesn’t break the database, whereas in SQL it would have thrown a fit.
“In MongoDB, how do I use ‘projection’ to hide the _id field in my search
results?”
Submission Visual
Section titled “Submission Visual”Take a screenshot of your terminal showing the output of your Tier 3 Audit Query.
“If you delete the library database by mistake, don’t come crying to me.
That’s why we call it a ‘Drop’ command. Once you drop it, it breaks.”
⏭ The Codex
Section titled “⏭ The Codex”A quick reference guide for your new MongoDB commands.