0
0
MongoDBquery~30 mins

Rows vs documents thinking in MongoDB - Hands-On Comparison

Choose your learning style9 modes available
Understanding Rows vs Documents Thinking in MongoDB
📖 Scenario: You are working for a small bookstore that wants to store information about books and their authors. The bookstore is moving from a traditional table-based database to MongoDB, which uses documents instead of rows.Your task is to create a simple MongoDB collection that shows how data is stored as documents, not rows, and understand the difference in thinking.
🎯 Goal: Build a MongoDB collection named books with documents that include book titles, authors, and publication years. Learn how to think in documents instead of rows.
📋 What You'll Learn
Create a MongoDB collection named books.
Insert three book documents with fields: title, author, and year.
Add a configuration variable minYear to filter books published after a certain year.
Write a query to find all books published after minYear.
Add a final step to count how many books match the filter.
💡 Why This Matters
🌍 Real World
Many modern applications use MongoDB to store data as documents, which is more flexible than traditional rows. This project helps beginners understand this difference.
💼 Career
Understanding document-based databases like MongoDB is essential for roles in backend development, data engineering, and database administration.
Progress0 / 4 steps
1
Create the books collection with three book documents
Create a MongoDB collection called books and insert exactly these three documents: { title: "The Hobbit", author: "J.R.R. Tolkien", year: 1937 }, { title: "1984", author: "George Orwell", year: 1949 }, and { title: "To Kill a Mockingbird", author: "Harper Lee", year: 1960 }.
MongoDB
Need a hint?

Use db.books.insertMany() with an array of three objects.

2
Add a variable minYear to filter books published after a certain year
Create a variable called minYear and set it to 1940. This will be used to find books published after 1940.
MongoDB
Need a hint?

Use const minYear = 1940 to create the variable.

3
Write a query to find books published after minYear
Write a MongoDB query using db.books.find() to find all books where the year field is greater than minYear. Use the query { year: { $gt: minYear } }.
MongoDB
Need a hint?

Use db.books.find({ year: { $gt: minYear } }) and assign it to booksAfterMinYear.

4
Count how many books match the filter
Add a line to count the number of books found by the query and store it in a variable called countBooks. Use booksAfterMinYear.count().
MongoDB
Need a hint?

Use booksAfterMinYear.count() to get the count.