Collections vs tables mental model in MongoDB - Performance Comparison
Start learning this pattern below
Jump into concepts and practice - no test required
When working with databases, it's helpful to understand how collections in MongoDB compare to tables in traditional databases.
We want to see how operations on collections grow as data size increases, similar to tables.
Analyze the time complexity of querying all documents from a MongoDB collection.
db.users.find({})
.toArray()
.then(users => {
// process users
})
This code fetches all documents from the 'users' collection and converts them to an array.
Look for repeated actions that affect performance.
- Primary operation: Reading each document in the collection.
- How many times: Once for every document in the collection.
As the number of documents grows, the time to read all documents grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 document reads |
| 100 | 100 document reads |
| 1000 | 1000 document reads |
Pattern observation: The work grows directly with the number of documents.
Time Complexity: O(n)
This means the time to read all documents grows linearly with the number of documents.
[X] Wrong: "Fetching all documents is always fast regardless of collection size."
[OK] Correct: As the collection grows, reading every document takes more time, so it slows down.
Understanding how collections behave like tables helps you explain database performance clearly and confidently.
"What if we add an index to the collection? How would the time complexity of searching change?"
Practice
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]
- Thinking collections have fixed columns like tables
- Assuming collections require strict schemas
- Believing collections cannot hold multiple documents
users in MongoDB?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]
- Using SQL syntax like CREATE TABLE
- Trying to call create() on collection name
- Omitting quotes around collection name
products with documents: {"name": "Pen", "price": 1.5}{"name": "Notebook", "price": 3.0, "color": "blue"}What will
db.products.find({}) return?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]
- Thinking find({}) filters by fields
- Expecting errors due to different document structures
- Assuming only documents with certain fields are returned
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]
- Assuming MongoDB requires fixed schema
- Not creating collection first (MongoDB auto-creates collections)
- Believing MongoDB forbids multiple inserts
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]
- Trying to enforce strict SQL schema in MongoDB
- Splitting columns into separate collections
- Storing all rows in one big document
