0
0
MongoDBquery~10 mins

Rows vs documents thinking in MongoDB - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Rows vs documents thinking
Start: Data to store
Choose storage style
Rows
Table
Fixed columns
Each row = 1 record
Query data accordingly
This flow shows how data can be stored as rows in tables or as documents in collections, highlighting the difference in structure and flexibility.
Execution Sample
MongoDB
db.users.insertOne({name: "Alice", age: 30, hobbies: ["reading", "hiking"]})
db.users.find({name: "Alice"})
Insert a document with flexible fields into a MongoDB collection and then query it by name.
Execution Table
StepActionData StateResult
1Insert document {name: 'Alice', age: 30, hobbies: ['reading', 'hiking']}Collection emptyDocument added as one record
2Query documents where name = 'Alice'Collection has 1 documentReturns the inserted document
3Insert document {name: 'Bob', age: 25}Collection has 1 documentDocument added, no hobbies field needed
4Query all documentsCollection has 2 documentsReturns both documents with their fields
5Attempt to query by hobbiesCollection has 2 documentsReturns only documents with hobbies field
6EndNo further actionsExecution stops
💡 No more queries or inserts; demonstration complete
Variable Tracker
VariableStartAfter Step 1After Step 3Final
Collection 'users'empty[{name: 'Alice', age: 30, hobbies: ['reading', 'hiking']}][{name: 'Alice', age: 30, hobbies: ['reading', 'hiking']}, {name: 'Bob', age: 25}]same
Key Moments - 2 Insights
Why can documents have different fields while rows cannot?
Documents in MongoDB are flexible and can have different fields per record, as shown in steps 1 and 3 where 'hobbies' is present only in Alice's document. Rows in tables require fixed columns for all records.
What happens when you query a field that some documents don't have?
As in step 5, querying by 'hobbies' returns only documents that have that field. Documents without it are excluded, unlike rows where all columns exist for every row.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the state of the collection after step 3?
AEmpty collection
BContains one document with name 'Alice' only
CContains two documents: one for 'Alice' and one for 'Bob'
DContains one document with name 'Bob' only
💡 Hint
Check the 'Data State' column in row for step 3 in the execution table
At which step does the query return only documents that have the 'hobbies' field?
AStep 5
BStep 2
CStep 4
DStep 1
💡 Hint
Look at the 'Result' column for step 5 in the execution table
If we added a new document with an extra field 'email', how would the collection state change?
AAll documents would have the 'email' field added
BOnly the new document would have the 'email' field
CThe collection would reject the new document
DThe 'email' field would replace 'hobbies' in all documents
💡 Hint
Recall that MongoDB documents can have flexible fields as shown in steps 1 and 3
Concept Snapshot
Rows vs Documents Thinking:
- Rows: fixed columns, each row same structure
- Documents: flexible fields, each document can differ
- Rows stored in tables; documents in collections
- Query returns full rows or matching documents
- Documents allow nested data and arrays
- Choose based on data flexibility needs
Full Transcript
This visual execution shows how data storage differs between rows and documents. Rows are fixed in structure, stored in tables, where each row has the same columns. Documents, like in MongoDB, are flexible and stored in collections. Each document can have different fields, including nested arrays. The example inserts documents with different fields and queries them, showing how queries return only matching documents. This helps understand why documents are more flexible than rows and how queries behave differently.