Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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
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
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
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
Hint
Use booksAfterMinYear.count() to get the count.
Practice
(1/5)
1. Which statement best describes the difference between rows in SQL and documents in MongoDB?
easy
A. Rows are flexible and can change structure easily; documents are rigid.
B. Rows can store nested data; documents only store flat data.
C. Rows have fixed columns; documents can have varied fields and nested data.
D. Rows and documents are exactly the same in structure and use.
Solution
Step 1: Understand row structure in SQL
Rows have fixed columns defined by the table schema, so each row has the same fields.
Step 2: Understand document structure in MongoDB
Documents can have different fields and nested objects, allowing flexible and varied data.
Final Answer:
Rows have fixed columns; documents can have varied fields and nested data. -> Option C
Thinking documents must have same fields like rows
Assuming rows can store nested data easily
Confusing flexibility of documents with rows
2. Which of the following is the correct way to insert a document with nested fields in MongoDB?
easy
A. db.collection.insertOne({name: 'Alice', address: {city: 'NY', zip: 10001}})
B. INSERT INTO collection VALUES ('Alice', {city: 'NY', zip: 10001})
C. db.collection.insertOne(name: 'Alice', address: 'NY')
D. INSERT document {name: 'Alice', address: {city: 'NY', zip: 10001}}
Solution
Step 1: Identify MongoDB insert syntax
MongoDB uses db.collection.insertOne() with a JSON-like document as argument.
Step 2: Check nested field format
Nested fields are represented as nested objects inside the document, like address: {city: 'NY', zip: 10001}.
Final Answer:
db.collection.insertOne({name: 'Alice', address: {city: 'NY', zip: 10001}}) -> Option A
Quick Check:
Correct MongoDB insert with nested document [OK]
Hint: Use insertOne() with nested JSON object for documents [OK]
Common Mistakes:
Using SQL INSERT syntax in MongoDB
Passing fields outside an object
Not using curly braces for nested fields
3. Given the collection users with documents like {name: 'Bob', scores: [10, 20, 30]}, what will db.users.find({scores: 20}) return?
medium
A. All documents where scores array contains 20
B. Documents where scores equals exactly 20
C. Documents where scores is missing
D. Syntax error because scores is an array
Solution
Step 1: Understand MongoDB query on array fields
Querying {scores: 20} matches documents where the array 'scores' contains the value 20.
Step 2: Apply to example document
Document with scores: [10, 20, 30] contains 20, so it matches and will be returned.
Final Answer:
All documents where scores array contains 20 -> Option A
Quick Check:
Query matches array elements [OK]
Hint: Querying array field matches if any element equals value [OK]
Common Mistakes:
Thinking query matches whole array exactly
Expecting syntax error for array query
Confusing missing field with array content
4. You run this MongoDB query: db.orders.find({items: {product: 'Book'}}) but get no results. What is the likely problem?
medium
A. The field name 'product' is misspelled.
B. The query expects items to be an object, but items is an array of objects.
C. MongoDB does not support querying nested fields.
D. The collection name 'orders' is incorrect.
Solution
Step 1: Analyze query structure
The query matches documents where 'items' exactly equals {product: 'Book'}.
Step 2: Understand data structure
If 'items' is an array of objects, the query must use dot notation or $elemMatch to match inside array elements.
Final Answer:
The query expects items to be an object, but items is an array of objects. -> Option B
Quick Check:
Querying array needs $elemMatch or dot notation [OK]
Hint: Use $elemMatch or dot notation to query inside arrays [OK]
Common Mistakes:
Querying array field as if it was a single object
Assuming MongoDB can't query nested fields
Ignoring data structure of the field
5. You want to store customer orders where each order can have multiple items with different quantities and prices. Which data model fits best in MongoDB?
hard
A. One document per item, with order info duplicated in each document.
B. Separate collections for orders and items, no embedding.
C. One flat table with one row per item, no nesting.
D. One document per order, with an array of item objects inside each document.
Solution
Step 1: Understand MongoDB document flexibility
MongoDB documents can embed arrays of objects, ideal for storing multiple items inside one order document.
Step 2: Compare options for data modeling
Embedding items inside order documents keeps related data together and fits MongoDB's document model better than duplication or flat tables.
Final Answer:
One document per order, with an array of item objects inside each document. -> Option D
Quick Check:
Embed related data in arrays for flexible documents [OK]
Hint: Embed related items as arrays inside order documents [OK]