Why modeling decisions matter in MongoDB - Performance Analysis
When we design how data is stored in MongoDB, it affects how fast queries run.
We want to see how these design choices change the work the database does as data grows.
Analyze the time complexity of this query on two different data models.
// Model 1: Embedded documents
db.users.find({ "orders.product": "book" })
// Model 2: Referenced documents
// First find order IDs
const orderIds = db.orders.find({ product: "book" }).map(o => o._id);
// Then find users with those orders
const users = db.users.find({ orders: { $in: orderIds } });
This code shows two ways to find users who ordered a book: one with embedded orders, one with separate order documents.
Look at what repeats as data grows.
- Primary operation: Scanning orders inside users or scanning orders collection.
- How many times: Once per user in embedded model; once per order in referenced model.
As the number of users and orders grows, the work changes differently.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 users, 50 orders | Scan 10 users with embedded orders |
| 100 users, 500 orders | Scan 100 users or 500 orders separately |
| 1000 users, 5000 orders | Scan 1000 users or 5000 orders separately |
Pattern observation: Embedded model work grows with users; referenced model work grows with orders.
Time Complexity: O(n)
This means the query time grows linearly with the number of documents scanned, depending on the model.
[X] Wrong: "Embedding data always makes queries faster."
[OK] Correct: Sometimes embedding means scanning many nested items per document, which can slow queries if data is large.
Understanding how data design affects query speed shows you think about real database work, a key skill for any developer.
"What if we added indexes on the embedded fields or referenced fields? How would that change the time complexity?"
Practice
Solution
Step 1: Understand data model impact
The data model determines how data is stored and accessed, affecting speed and ease of use.Step 2: Eliminate unrelated options
Options about colors, fonts, or internet speed do not relate to data modeling.Final Answer:
It affects how fast and easy your app can access data -> Option CQuick Check:
Data model affects app speed and ease [OK]
- Confusing UI features with data modeling
- Thinking data model affects internet speed
- Ignoring performance impact of modeling
Solution
Step 1: Identify embedding format
Embedding means placing a sub-document inside the main document as a nested object.Step 2: Check options for nested object
{ name: 'Alice', address: { city: 'NY', zip: '10001' } } uses a nested object for address, which is correct embedding syntax.Final Answer:
{ name: 'Alice', address: { city: 'NY', zip: '10001' } } -> Option AQuick Check:
Embedding uses nested objects [OK]
- Using array instead of object for embedding
- Storing address as a plain string
- Referencing with ObjectId instead of embedding
{ _id: 1, name: 'John', orders: [{ product: 'Book', qty: 2 }, { product: 'Pen', qty: 5 }] }{ _id: 2, name: 'Jane', orders: [{ product: 'Notebook', qty: 1 }] }db.users.find({ 'orders.product': 'Book' })Solution
Step 1: Understand query condition
The query looks for documents where any order's product is 'Book'.Step 2: Match documents with 'Book' product
Only John has an order with product 'Book', so only his document matches.Final Answer:
[{ _id: 1, name: 'John', orders: [...] }] -> Option AQuick Check:
Query matches embedded product 'Book' [OK]
- Assuming query matches documents without 'Book'
- Expecting syntax error from dot notation
- Confusing array matching with string matching
db.users.updateOne(
{ _id: 1, comments.id: 5 },
{ $set: { 'comments.text': 'Updated' } }
)Solution
Step 1: Analyze update query
The filter matches the comment with id 5, but the update sets 'comments.text' without specifying which array element.Step 2: Identify missing positional operator
To update a specific array element, the $ positional operator is needed in the update path: 'comments.$.text'.Final Answer:
Missing $ positional operator to target the correct array element -> Option DQuick Check:
Use $ to update matched array element [OK]
- Forgetting $ in update path for arrays
- Changing updateOne to updateMany unnecessarily
- Assuming collection name or filter syntax is wrong
Solution
Step 1: Analyze data size and access pattern
Comments are large and often not needed when reading posts, so embedding would make posts heavy.Step 2: Choose referencing for efficiency
Referencing comments separately keeps post documents small and loads comments only when needed.Final Answer:
Store comments in a separate collection and reference posts to avoid large post documents -> Option BQuick Check:
Large optional data = use referencing [OK]
- Embedding large optional data causing slow reads
- Storing comments as strings losing structure
- Duplicating data causing inconsistency
Ready to prove it?
Pass the test and this pattern moves unknown
