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
Collections vs Tables Mental Model in MongoDB
📖 Scenario: You are helping a small bookstore organize its data. The bookstore wants to store information about books and customers. You will create a MongoDB collection to hold book data, similar to how a table holds data in a traditional database.
🎯 Goal: Build a MongoDB collection named books that stores documents representing books with fields for title, author, and year. Then, add a configuration variable to filter books published after a certain year. Finally, write a query to find those books and complete the setup by creating an index on the author field.
📋 What You'll Learn
Create a MongoDB collection named books with 3 book documents
Add a variable year_threshold to filter books published after this year
Write a query to find books with year greater than year_threshold
Create an index on the author field in the books collection
💡 Why This Matters
🌍 Real World
Understanding collections and documents in MongoDB helps you organize data for real applications like bookstores, libraries, or online shops.
💼 Career
Many companies use MongoDB for flexible data storage. Knowing how collections work compared to tables is essential for database roles and backend development.
Progress0 / 4 steps
1
Create the books collection with 3 book documents
Create a MongoDB collection called books and insert exactly these 3 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([...]) to add multiple documents at once.
2
Add a variable year_threshold to filter books
Create a variable called year_threshold and set it to 1940. This will be used to find books published after this year.
MongoDB
Hint
Use const year_threshold = 1940 to create the variable.
3
Write a query to find books published after year_threshold
Write a MongoDB query using db.books.find() to find all books where the year field is greater than the variable year_threshold.
MongoDB
Hint
Use db.books.find({ year: { $gt: year_threshold } }) to find books published after the threshold year.
4
Create an index on the author field
Create an index on the author field in the books collection using db.books.createIndex().
MongoDB
Hint
Use db.books.createIndex({ author: 1 }) to create an ascending index on the author field.
Practice
(1/5)
1. Which of the following best describes a MongoDB collection compared to a SQL table?
easy
A. A collection requires a strict schema like SQL tables.
B. A collection stores data in fixed rows and columns.
C. A collection stores flexible documents without fixed columns.
D. A collection cannot store multiple records.
Solution
Step 1: Understand SQL tables
SQL tables store data in rows and columns with a fixed schema, meaning each row has the same columns.
Step 2: Understand MongoDB collections
MongoDB collections store documents that can have different fields and structures, so they are flexible and schema-less.
Final Answer:
A collection stores flexible documents without fixed columns. -> Option C
Quick Check:
Collections = flexible documents [OK]
Hint: Collections are flexible; tables have fixed columns [OK]
Common Mistakes:
Thinking collections have fixed columns like tables
Assuming collections require strict schemas
Believing collections cannot hold multiple documents
2. Which of the following is the correct way to create a collection named users in MongoDB?
easy
A. db.createCollection('users');
B. db.users.create();
C. CREATE TABLE users;
D. CREATE COLLECTION users;
Solution
Step 1: Recall MongoDB syntax for creating collections
MongoDB uses the method db.createCollection('name') to create a collection explicitly.
Step 2: Check other options
CREATE COLLECTION users; and C use SQL syntax, which is invalid in MongoDB. db.users.create(); is not a valid MongoDB command.
A. All documents including fields like name, price, and color if present.
B. Only documents with the price field.
C. Only documents with the color field.
D. An error because documents have different fields.
Solution
Step 1: Understand find({}) query
The query find({}) returns all documents in the collection regardless of their fields.
Step 2: Check document fields
Documents can have different fields in MongoDB collections; this does not cause errors or filtering.
Final Answer:
All documents including fields like name, price, and color if present. -> Option A
Quick Check:
find({}) returns all documents [OK]
Hint: find({}) returns all documents regardless of fields [OK]
Common Mistakes:
Thinking find({}) filters by fields
Expecting errors due to different document structures
Assuming only documents with certain fields are returned
4. You tried to insert documents with different fields into a MongoDB collection but got an error. What is the most likely cause?
medium
A. MongoDB collections require all documents to have the same fields.
B. MongoDB does not allow inserting multiple documents.
C. You forgot to create the collection before inserting.
D. You used SQL INSERT syntax instead of MongoDB insert methods.
Solution
Step 1: Recall MongoDB insert syntax
MongoDB uses insertOne() or insertMany() methods, not SQL INSERT statements.
Step 2: Understand document flexibility
MongoDB collections allow documents with different fields; this does not cause errors.
Final Answer:
You used SQL INSERT syntax instead of MongoDB insert methods. -> Option D
Quick Check:
MongoDB insert = insertOne()/insertMany() [OK]
Hint: Use MongoDB insertOne()/insertMany(), not SQL INSERT [OK]
Common Mistakes:
Assuming MongoDB requires fixed schema
Not creating collection first (MongoDB auto-creates collections)
Believing MongoDB forbids multiple inserts
5. You want to migrate a SQL table with fixed columns to MongoDB. Which approach best fits MongoDB's collection model?
hard
A. Keep the exact SQL schema and enforce it strictly in MongoDB.
B. Store each row as a document with the same fields, but allow optional fields for future flexibility.
C. Convert the table into multiple collections, each with one column as documents.
D. Store all rows as a single large document with arrays for each column.
Solution
Step 1: Understand MongoDB document flexibility
MongoDB collections store documents that can have different fields, so keeping the same fields but allowing optional ones fits well.
Step 2: Evaluate other options
Convert the table into multiple collections, each with one column as documents. breaks data into many collections unnecessarily. Keep the exact SQL schema and enforce it strictly in MongoDB. tries to enforce strict schema, which MongoDB does not require. Store all rows as a single large document with arrays for each column. stores all data in one document, which is inefficient and not recommended.
Final Answer:
Store each row as a document with the same fields, but allow optional fields for future flexibility. -> Option B
Quick Check:
MongoDB collections = flexible documents per row [OK]
Hint: Migrate rows as flexible documents with optional fields [OK]