One-to-one embedding pattern in MongoDB - Time & Space Complexity
When using one-to-one embedding in MongoDB, we want to know how the time to get data changes as the data grows.
We ask: How does fetching a document with embedded data scale when the number of documents increases?
Analyze the time complexity of the following MongoDB query.
// Find a user and get their profile embedded inside
db.users.findOne({ _id: userId })
This query fetches one user document that includes an embedded profile document inside it.
Look for repeated steps in the query process.
- Primary operation: Searching the users collection for one document by _id.
- How many times: This happens once per query, no loops or repeated scans.
As the number of users grows, how does the time to find one user change?
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Few operations, quick lookup |
| 100 | Still very quick, almost same time |
| 1000 | Lookup time stays about the same |
Pattern observation: Because the query uses a unique _id index, the time stays almost constant no matter how many users there are.
Time Complexity: O(1)
This means fetching one user with embedded data takes about the same time no matter how many users exist.
[X] Wrong: "Fetching embedded data gets slower as the collection grows because it has to look through all documents."
[OK] Correct: The query uses a unique _id index, so it directly finds the document without scanning all documents.
Understanding how embedding affects query speed helps you design fast and simple data models, a skill valued in real projects and interviews.
"What if the embedded document was stored in a separate collection and you needed to join it? How would the time complexity change?"
Practice
one-to-one embedding pattern in MongoDB?Solution
Step 1: Understand one-to-one embedding
One-to-one embedding stores related data inside the same document instead of separate collections.Step 2: Identify the advantage
This approach simplifies queries and improves read speed by avoiding joins.Final Answer:
It stores related data inside a single document for faster reads. -> Option CQuick Check:
Embedding = faster reads [OK]
- Thinking embedding splits data into multiple collections
- Assuming embedding requires joins
- Confusing embedding with data duplication
profile document inside a user document in MongoDB?Solution
Step 1: Check embedding syntax
Embedding means nesting a document inside another document as a field with an object value.Step 2: Identify correct JSON structure
{ _id: 1, name: 'Alice', profile: { age: 30, city: 'NY' } } correctly nests the profile as an object inside user. { _id: 1, name: 'Alice' }, profile: { age: 30, city: 'NY' } separates profile outside user. { _id: 1, name: 'Alice', profile: [ { age: 30, city: 'NY' } ] } uses an array unnecessarily. { _id: 1, name: 'Alice', profile: 'age:30, city:NY' } stores profile as a string, not an object.Final Answer:
{ _id: 1, name: 'Alice', profile: { age: 30, city: 'NY' } } -> Option BQuick Check:
Embedded document = nested object [OK]
- Placing embedded document outside main document
- Using array instead of object for one-to-one
- Storing embedded data as string
{ _id: 1, name: 'Bob', address: { street: 'Main St', city: 'LA' } }, what will be the result of the query db.users.findOne({ _id: 1 })?Solution
Step 1: Understand findOne query
The query searches for a document with _id equal to 1 and returns the whole document.Step 2: Check document structure
The document includes embedded address inside user, so the full document is returned.Final Answer:
{ _id: 1, name: 'Bob', address: { street: 'Main St', city: 'LA' } } -> Option DQuick Check:
findOne returns full document [OK]
- Expecting only top-level fields returned
- Thinking embedded fields are separate documents
- Assuming query returns null if embedded exists
{ _id: 2, name: 'Carol', profile: { age: 25, city: 'Chicago' } }. Which update command correctly changes the city to 'Boston' using one-to-one embedding?Solution
Step 1: Identify correct update syntax for embedded field
To update a nested field, use dot notation like 'profile.city'.Step 2: Check each option
db.users.updateOne({ _id: 2 }, { $set: { 'profile.city': 'Boston' } }) uses correct dot notation. db.users.updateOne({ _id: 2 }, { $set: { profile: 'Boston' } }) replaces entire profile with string. db.users.updateOne({ _id: 2 }, { $set: { city: 'Boston' } }) tries to set city at top level, which doesn't exist. db.users.updateOne({ _id: 2 }, { $set: { 'profile[city]': 'Boston' } }) uses invalid bracket notation.Final Answer:
db.users.updateOne({ _id: 2 }, { $set: { 'profile.city': 'Boston' } }) -> Option AQuick Check:
Use dot notation to update embedded fields [OK]
- Replacing whole embedded document instead of one field
- Using incorrect field path without dot notation
- Using invalid bracket notation in update
Solution
Step 1: Analyze data relationship and size
Passports are small and tightly connected to users, perfect for embedding.Step 2: Choose embedding benefits
Embedding keeps related data together, simplifies queries, and improves read speed.Step 3: Evaluate other options
Store passports in a separate collection and reference user IDs to avoid large documents adds complexity with references. Duplicate passport data in both user and passport collections for faster writes duplicates data causing inconsistency. Store passport as a string field inside user document to simplify queries stores data as string losing structure.Final Answer:
Embed passport details inside user document because passports are small and tightly linked. -> Option AQuick Check:
Small, tightly linked data = embed [OK]
- Choosing references for small one-to-one data
- Duplicating data causing inconsistency
- Storing structured data as plain string
Ready to prove it?
Pass the test and this pattern moves unknown
