Bird
Raised Fist0
MongoDBquery~20 mins

Embedded documents (nested objects) in MongoDB - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Embedded Documents Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Find documents with a nested field value
Given a collection users where each document has an embedded address object with fields city and zip, which query returns all users living in the city 'Springfield'?
MongoDB
db.users.find({"address.city": "Springfield"})
Adb.users.find({"address": {"city": "Springfield"}})
Bdb.users.find({address: {city: "Springfield"}})
Cdb.users.find({"address.city": "Springfield"})
Ddb.users.find({city: "Springfield"})
Attempts:
2 left
💡 Hint
Use dot notation to query nested fields inside embedded documents.
query_result
intermediate
2:00remaining
Retrieve nested array elements in embedded documents
Consider a collection orders where each document has an embedded array items with objects containing product and quantity. Which query returns all orders that include an item with product equal to 'Pen'?
MongoDB
db.orders.find({"items.product": "Pen"})
Adb.orders.find({product: "Pen"})
Bdb.orders.find({items: {product: "Pen"}})
Cdb.orders.find({"items": {"product": "Pen"}})
Ddb.orders.find({"items.product": "Pen"})
Attempts:
2 left
💡 Hint
Use dot notation to query fields inside objects within arrays.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in nested document update
Which update command correctly sets the nested field profile.contact.email to 'user@example.com' for the document with _id 1?
MongoDB
db.users.updateOne({_id: 1}, {$set: {"profile.contact.email": "user@example.com"}})
A)}}"moc.elpmaxe@resu" :"liame.tcatnoc.eliforp"{ :tes${ ,}1 :di_{(enOetadpu.sresu.bd
Bdb.users.updateOne({_id: 1}, {$set: {"profile.contact.email": "user@example.com"}})
Cdb.users.updateOne({_id: 1}, {$set: {profile: {contact: {email: "user@example.com"}}}})
Ddb.users.updateOne({_id: 1}, {$set: {"profile": {"contact": {"email": "user@example.com"}}}})
Attempts:
2 left
💡 Hint
Use quotes around the full nested field path when using dot notation in update operators.
optimization
advanced
2:00remaining
Optimize query on nested fields with indexes
You have a collection products with embedded documents specs containing weight and color. Which index will best optimize queries filtering by specs.color?
A{"specs.color": 1}
B{"specs": 1}
C{"color": 1}
D{"specs.weight": 1}
Attempts:
2 left
💡 Hint
Indexes on nested fields use dot notation for the field name.
🧠 Conceptual
expert
2:00remaining
Understanding document size limits with embedded documents
MongoDB has a maximum document size of 16MB. Which statement about using embedded documents is true?
AEmbedding large nested documents can cause the document to exceed the 16MB limit, so large data should be referenced instead.
BEmbedded documents do not count towards the 16MB document size limit.
CMongoDB automatically splits large embedded documents into multiple documents to avoid size limits.
DThe 16MB limit applies only to top-level fields, not to embedded documents.
Attempts:
2 left
💡 Hint
Consider how document size limits affect embedded data storage.

Practice

(1/5)
1. What is an embedded document in MongoDB?
easy
A. A document stored inside another document as a nested object
B. A separate collection linked by an ID
C. A document stored in a different database
D. A document stored as a file on disk

Solution

  1. Step 1: Understand MongoDB document structure

    MongoDB stores data in documents, which can contain nested objects called embedded documents.
  2. Step 2: Identify embedded document meaning

    An embedded document is a document inside another document, not a separate collection or file.
  3. Final Answer:

    A document stored inside another document as a nested object -> Option A
  4. Quick Check:

    Embedded document = nested object inside document [OK]
Hint: Think of a box inside another box holding related info [OK]
Common Mistakes:
  • Confusing embedded documents with references
  • Thinking embedded documents are separate collections
  • Assuming embedded documents are stored outside the database
2. Which of the following is the correct way to insert an embedded document in MongoDB?
easy
A. db.collection.insertOne([{name: 'Alice'}, {address: {city: 'NY', zip: 10001}}])
B. db.collection.insertOne({name: 'Alice'}, {address: {city: 'NY', zip: 10001}})
C. db.collection.insertOne(name: 'Alice', address: {city: 'NY', zip: 10001})
D. db.collection.insertOne({name: 'Alice', address: {city: 'NY', zip: 10001}})

Solution

  1. Step 1: Review MongoDB insertOne syntax

    insertOne takes a single document object with fields and values, including nested objects.
  2. Step 2: Check each option's syntax

    db.collection.insertOne({name: 'Alice', address: {city: 'NY', zip: 10001}}) correctly nests the address object inside the main document. Others have syntax errors or wrong structure.
  3. Final Answer:

    db.collection.insertOne({name: 'Alice', address: {city: 'NY', zip: 10001}}) -> Option D
  4. Quick Check:

    Nested object inside one document = correct insert [OK]
Hint: Use one object with nested braces for embedded docs [OK]
Common Mistakes:
  • Passing multiple objects instead of one
  • Missing curly braces around nested document
  • Using array instead of object for embedded document
3. Given the document { name: 'Bob', contact: { email: 'bob@example.com', phone: '1234' } }, what will the query db.users.find({ 'contact.email': 'bob@example.com' }) return?
medium
A. No documents, because nested fields can't be queried
B. All documents where contact.email equals 'bob@example.com'
C. Documents where name equals 'bob@example.com'
D. Documents where contact is exactly 'bob@example.com'

Solution

  1. Step 1: Understand dot notation in queries

    MongoDB uses dot notation to query fields inside embedded documents.
  2. Step 2: Analyze the query

    The query looks for documents where the embedded field contact.email matches the given value.
  3. Final Answer:

    All documents where contact.email equals 'bob@example.com' -> Option B
  4. Quick Check:

    Dot notation queries embedded fields correctly [OK]
Hint: Use dot notation to access nested fields in queries [OK]
Common Mistakes:
  • Trying to match entire embedded document instead of field
  • Using wrong field name without dot notation
  • Assuming nested fields can't be queried
4. What is wrong with this update query to change the city in an embedded address document?
db.users.updateOne({name: 'Eve'}, {address.city: 'LA'})
medium
A. The field name should not use dot notation
B. The query filter is incorrect
C. The update document is missing the $set operator
D. updateOne cannot update embedded documents

Solution

  1. Step 1: Recall updateOne syntax

    updateOne requires an update operator like $set to specify fields to change.
  2. Step 2: Identify missing $set operator

    The query tries to update address.city directly without $set, which is invalid syntax.
  3. Final Answer:

    The update document is missing the $set operator -> Option C
  4. Quick Check:

    Updates need $set for field changes [OK]
Hint: Always use $set to update fields, including nested ones [OK]
Common Mistakes:
  • Forgetting $set in update document
  • Using dot notation incorrectly in filter
  • Thinking updateOne can't change nested fields
5. You want to store multiple phone numbers inside a user's document using embedded documents. Which schema design is best?
hard
A. { name: 'Sam', phones: [{ type: 'home', number: '111' }, { type: 'work', number: '222' }] }
B. { name: 'Sam', phone1: '111', phone2: '222' }
C. { name: 'Sam', phones: { home: '111', work: '222' } }
D. { name: 'Sam', phones: '111,222' }

Solution

  1. Step 1: Understand storing multiple embedded documents

    To store multiple related items, use an array of embedded documents for flexibility and clarity.
  2. Step 2: Compare options

    { name: 'Sam', phones: [{ type: 'home', number: '111' }, { type: 'work', number: '222' }] } uses an array of objects with type and number, which is clear and scalable. Others are less flexible or harder to query.
  3. Final Answer:

    { name: 'Sam', phones: [{ type: 'home', number: '111' }, { type: 'work', number: '222' }] } -> Option A
  4. Quick Check:

    Array of embedded docs best for multiple related items [OK]
Hint: Use arrays of objects for multiple related embedded documents [OK]
Common Mistakes:
  • Storing multiple values as comma-separated string
  • Using separate fields for each phone number
  • Using a single object without array for multiple items