0
0
MongoDBquery~30 mins

Tables vs collections thinking in MongoDB - Hands-On Comparison

Choose your learning style9 modes available
Understanding Tables vs Collections Thinking in MongoDB
📖 Scenario: You are helping a small bookstore organize its data. The bookstore wants to store information about books and their authors. You will practice thinking in terms of tables (like in SQL) and collections (in MongoDB) to understand how data is structured differently.
🎯 Goal: Build a MongoDB collection that stores books with embedded author information, practicing the collection thinking approach instead of separate tables.
📋 What You'll Learn
Create a collection named books with documents for three books.
Each book document must include title, year, and an embedded author document.
The author document must have name and birthYear fields.
Create a variable minYear to filter books published after this year.
Write a MongoDB query to find all books published after minYear.
Add a projection to show only the title and author.name fields in the query result.
💡 Why This Matters
🌍 Real World
Many modern applications use MongoDB to store data as collections of documents. Understanding how to structure data and query it is essential for building flexible and scalable apps.
💼 Career
Database developers and backend engineers often work with MongoDB. Knowing how to think in collections and write queries helps you design efficient data models and retrieve data quickly.
Progress0 / 4 steps
1
Create the books collection with three book documents
Create a variable called books and assign it a list of three documents. Each document must have these exact fields and values:
1. { title: "The Great Gatsby", year: 1925, author: { name: "F. Scott Fitzgerald", birthYear: 1896 } }
2. { title: "1984", year: 1949, author: { name: "George Orwell", birthYear: 1903 } }
3. { title: "To Kill a Mockingbird", year: 1960, author: { name: "Harper Lee", birthYear: 1926 } }
MongoDB
Need a hint?

Use a list of dictionaries. Each dictionary represents a book document with nested author info.

2
Create a variable minYear to filter books published after this year
Create a variable called minYear and set it to 1930.
MongoDB
Need a hint?

Just assign the number 1930 to the variable minYear.

3
Write a MongoDB query to find books published after minYear
Create a variable called query and assign it a MongoDB query document that finds books where the year field is greater than minYear. Use the MongoDB query operator $gt.
MongoDB
Need a hint?

Use { "year": { "$gt": minYear } } to find books published after minYear.

4
Add a projection to show only title and author.name fields
Create a variable called projection and assign it a MongoDB projection document that includes only the title and author.name fields. Use 1 to include fields and 0 to exclude others.
MongoDB
Need a hint?

Use { "title": 1, "author.name": 1, "_id": 0 } to include only these fields and exclude the _id field.