Why document databases over relational in MongoDB - Performance Analysis
Start learning this pattern below
Jump into concepts and practice - no test required
We want to understand how the time it takes to work with data changes when using document databases compared to relational databases.
Specifically, we ask: How does the structure of data affect the speed of common operations?
Analyze the time complexity of a simple MongoDB query that finds a document by its ID and updates a nested field.
// Find a user by ID and update their address city
const userId = ObjectId("12345");
db.users.updateOne(
{ _id: userId },
{ $set: { "address.city": "New City" } }
);
This code finds one user document by its unique ID and updates the city inside the nested address field.
Look for repeated steps or loops in the operation.
- Primary operation: Searching for a document by its unique ID.
- How many times: This happens once per query, no loops over multiple documents.
As the number of documents grows, finding by ID stays fast because of indexing.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Few operations, quick find |
| 100 | Still very few operations, quick find |
| 1000 | Still very few operations, quick find |
Pattern observation: The time to find by ID does not grow much as data grows because indexes help keep it fast.
Time Complexity: O(1)
This means the time to find and update a document by ID stays about the same no matter how many documents there are.
[X] Wrong: "Finding a document by ID gets slower as the database grows because it has to check every document."
[OK] Correct: Document databases use indexes on IDs, so they jump directly to the right document without checking all others.
Understanding how document databases handle queries efficiently shows you know how data structure and indexing affect speed. This skill helps you explain why certain databases fit some jobs better than others.
"What if we searched for a document by a field without an index? How would the time complexity change?"
Practice
Solution
Step 1: Understand data storage formats
Document databases store data as JSON-like documents, allowing flexible and dynamic structures.Step 2: Compare with relational databases
Relational databases require fixed schemas and tables, making changes harder.Final Answer:
Because document databases store data in flexible JSON-like documents that can change structure easily. -> Option AQuick Check:
Flexible JSON-like storage [OK]
- Thinking document DBs require fixed schemas
- Believing document DBs only handle numbers
- Assuming no indexing in document DBs
users?Solution
Step 1: Recall MongoDB insert syntax
MongoDB usesinsertOne()to add a single document to a collection.Step 2: Check options for correct syntax
db.users.insertOne({name: 'Alice', age: 30}) usesdb.users.insertOne({name: 'Alice', age: 30}), which is correct MongoDB syntax.Final Answer:
db.users.insertOne({name: 'Alice', age: 30}) -> Option CQuick Check:
MongoDB insertOne() [OK]
- Using SQL syntax in MongoDB
- Using non-existent methods like add()
- Writing commands as plain English
products collection:{ "_id": 1, "name": "Pen", "details": { "color": "blue", "price": 1.5 } }What will the query
db.products.find({"details.color": "blue"}) return?Solution
Step 1: Understand dot notation in queries
MongoDB allows querying nested fields using dot notation like "details.color".Step 2: Analyze the query and document
The document has a nested field details.color with value "blue", so the query matches this document.Final Answer:
All products with a details field containing color blue. -> Option AQuick Check:
Dot notation queries nested fields [OK]
- Thinking nested fields can't be queried
- Confusing top-level and nested fields
- Assuming query syntax is SQL-like
db.users.find({age: > 25})Why does this query fail and how to fix it?
Solution
Step 1: Identify MongoDB comparison operator syntax
MongoDB uses special operators like $gt for 'greater than' inside query objects.Step 2: Correct the query syntax
The correct query is{age: {$gt: 25}}, not using > directly.Final Answer:
The operator > should be inside $gt like {age: {$gt: 25}}. -> Option DQuick Check:
Use $gt for greater than in MongoDB queries [OK]
- Using > directly in query object
- Mixing SQL syntax with MongoDB
- Assuming >= fixes the error
Solution
Step 1: Understand data embedding in document databases
Document databases allow embedding related data (like comments) inside a single document (post).Step 2: Compare with relational approach
Relational databases store posts and comments in separate tables, requiring joins to combine them.Step 3: Benefits of embedding
Embedding comments inside posts reduces the need for joins and speeds up reading a post with its comments.Final Answer:
Because you can store each post and its comments together in one document, making reads faster. -> Option BQuick Check:
Embedding related data = faster reads [OK]
- Thinking relational DBs can't store comments
- Believing comments must be separate collections
- Assuming relational DBs lack indexing
