Collections and tables both store data, but in different database types. Understanding their difference helps you work with data easily.
Collections vs tables mental model in MongoDB
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
MongoDB
No direct code syntax since this is a concept comparison.A table is used in SQL databases and has rows and columns.
A collection is used in MongoDB and stores documents without fixed columns.
Examples
users with fixed columns.MongoDB
SQL Table example:
CREATE TABLE users (
id INT PRIMARY KEY,
name VARCHAR(100),
age INT
);users collection without fixed columns.MongoDB
MongoDB Collection example:
db.users.insertOne({ name: "Alice", age: 30 });Sample Program
This example shows how a MongoDB collection can store documents with different fields, unlike a SQL table.
MongoDB
use testdb // Insert documents into MongoDB collection db.users.insertMany([ { name: "Alice", age: 30 }, { name: "Bob", city: "New York" } ]); // Query all documents db.users.find().pretty();
Important Notes
Collections do not require a fixed structure, so documents can have different fields.
Tables require a fixed schema, so every row has the same columns.
MongoDB collections are more flexible for changing data needs.
Summary
Tables store data in rows and columns with a fixed schema.
Collections store flexible documents without fixed columns.
Understanding this helps when moving between SQL and MongoDB.
Practice
1. Which of the following best describes a MongoDB collection compared to a SQL table?
easy
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 CQuick 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
Solution
Step 1: Recall MongoDB syntax for creating collections
MongoDB uses the methoddb.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.Final Answer:
db.createCollection('users'); -> Option AQuick Check:
MongoDB collection creation = db.createCollection() [OK]
Hint: Use db.createCollection('name') to create collections [OK]
Common Mistakes:
- Using SQL syntax like CREATE TABLE
- Trying to call create() on collection name
- Omitting quotes around collection name
3. Given a MongoDB collection
What will
products with documents: {"name": "Pen", "price": 1.5}{"name": "Notebook", "price": 3.0, "color": "blue"}What will
db.products.find({}) return?medium
Solution
Step 1: Understand find({}) query
The queryfind({})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 AQuick 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
Solution
Step 1: Recall MongoDB insert syntax
MongoDB usesinsertOne()orinsertMany()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 DQuick 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
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 BQuick Check:
MongoDB collections = flexible documents per row [OK]
Hint: Migrate rows as flexible documents with optional fields [OK]
Common Mistakes:
- Trying to enforce strict SQL schema in MongoDB
- Splitting columns into separate collections
- Storing all rows in one big document
