Bird
Raised Fist0
MongoDBquery~20 mins

How MongoDB stores data as documents - Practice Exercises

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
🎖️
MongoDB Document Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
What does this MongoDB document look like after insertion?
Consider the following MongoDB insert operation:

db.users.insertOne({ name: "Alice", age: 30, hobbies: ["reading", "hiking"] })

What will the stored document look like when retrieved?
MongoDB
db.users.findOne({ name: "Alice" })
A{ "name": "Alice", "age": "30", "hobbies": "reading, hiking" }
B{ "_id": ObjectId(...), "name": "Alice", "age": 30, "hobbies": ["reading", "hiking"] }
C{ "_id": ObjectId(...), "name": "Alice", "age": "30", "hobbies": ["reading", "hiking"] }
D{ "name": "Alice", "age": 30, "hobbies": ["reading", "hiking"] }
Attempts:
2 left
💡 Hint
Remember MongoDB automatically adds an _id field and preserves data types.
🧠 Conceptual
intermediate
1:30remaining
How does MongoDB store nested data?
Which of the following best describes how MongoDB stores nested data inside documents?
AAs embedded documents within the main document using key-value pairs.
BAs flat JSON strings inside a single field.
CAs separate tables linked by foreign keys.
DAs separate files on disk referenced by the document.
Attempts:
2 left
💡 Hint
Think about how JSON objects can contain other objects.
📝 Syntax
advanced
2:30remaining
Which MongoDB query correctly updates a nested field?
Given a document structure:

{ name: "Bob", address: { city: "NY", zip: 10001 } }

Which query correctly updates the city to "LA"?
Adb.collection.updateOne({ name: "Bob" }, { $set: { address: { city: "LA" } } })
Bdb.collection.updateOne({ name: "Bob" }, { city: "LA" })
Cdb.collection.updateOne({ name: "Bob" }, { $set: { "address.city": "LA" } })
Ddb.collection.updateOne({ name: "Bob" }, { $update: { "address.city": "LA" } })
Attempts:
2 left
💡 Hint
Use dot notation with $set to update nested fields.
optimization
advanced
3:00remaining
How to optimize queries on nested document fields?
You have a collection with documents containing nested fields like:

{ user: { name: "Eve", age: 25 }, status: "active" }

Which approach best improves query performance filtering by user name?
AUse a text index on the entire document.
BCreate an index on the status field only.
CFlatten the document to remove nesting.
DCreate an index on the nested field user.name.
Attempts:
2 left
💡 Hint
Indexes on fields used in queries speed up searches.
🔧 Debug
expert
3:00remaining
Why does this MongoDB query return no results?
Given documents:

{ name: "Sam", details: { age: 40, city: "Boston" } }

Which query will NOT return this document?

Options:
A) db.collection.find({ "details.age": 40 })
B) db.collection.find({ details: { age: 40, city: "Boston" } })
C) db.collection.find({ details: { city: "Boston", age: 40 } })
D) db.collection.find({ "details.city": "Boston" })
AReturns no documents because field order in embedded document matters.
BReturns the document because it matches the nested city field.
CReturns the document because exact object match requires field order.
DReturns the document because it matches the nested age field.
Attempts:
2 left
💡 Hint
Exact matches on embedded documents require the same field order.

Practice

(1/5)
1. What is the basic unit of data storage in MongoDB?
easy
A. A document
B. A table
C. A row
D. A column

Solution

  1. Step 1: Understand MongoDB data structure

    MongoDB stores data as documents, not tables or rows like traditional databases.
  2. Step 2: Identify the correct data unit

    The document is the flexible data unit that holds fields and values in MongoDB.
  3. Final Answer:

    A document -> Option A
  4. Quick Check:

    MongoDB data unit = document [OK]
Hint: Remember: MongoDB uses documents, not tables or rows [OK]
Common Mistakes:
  • Confusing documents with tables from SQL
  • Thinking rows or columns are used in MongoDB
  • Assuming collections are the basic unit
2. Which of the following is the correct way to represent a MongoDB document with a field name and value Alice?
easy
A. [ 'name': 'Alice' ]
B. { 'name' = 'Alice' }
C. { name: 'Alice' }
D. ( name: 'Alice' )

Solution

  1. Step 1: Recall MongoDB document syntax

    MongoDB documents use JSON-like syntax with curly braces and colon to assign values.
  2. Step 2: Check each option's syntax

    { name: 'Alice' } uses correct JSON-like syntax: { name: 'Alice' }. Others use invalid symbols or brackets.
  3. Final Answer:

    { name: 'Alice' } -> Option C
  4. Quick Check:

    Correct MongoDB document = JSON-like with colons [OK]
Hint: Use curly braces and colons for fields in MongoDB documents [OK]
Common Mistakes:
  • Using equal signs instead of colons
  • Using square or round brackets instead of curly braces
  • Putting field names in quotes incorrectly
3. Given the MongoDB document: { name: 'Bob', age: 30, hobbies: ['reading', 'swimming'] }, what is the value of the hobbies field?
medium
A. A string 'reading, swimming'
B. A nested document with keys 'reading' and 'swimming'
C. A number 2
D. An array with 'reading' and 'swimming'

Solution

  1. Step 1: Identify the data type of hobbies field

    The hobbies field is shown with square brackets, which means it is an array.
  2. Step 2: Understand array contents

    The array contains two string elements: 'reading' and 'swimming'.
  3. Final Answer:

    An array with 'reading' and 'swimming' -> Option D
  4. Quick Check:

    Square brackets mean array = An array with 'reading' and 'swimming' [OK]
Hint: Square brackets [] mean array in MongoDB documents [OK]
Common Mistakes:
  • Thinking it's a string instead of an array
  • Confusing arrays with nested documents
  • Assuming the value is a count of items
4. Identify the error in this MongoDB document: { 'title': 'Book', pages: 250, author: 'John Doe' }
medium
A. This document is valid and has no error
B. Mixing quoted and unquoted field names is invalid
C. Values must be numbers only
D. Field names must all be unquoted

Solution

  1. Step 1: Check field name quoting rules

    MongoDB allows field names to be quoted or unquoted as long as they are valid strings.
  2. Step 2: Verify document validity

    Mixing quoted and unquoted field names is allowed; values are correctly typed.
  3. Final Answer:

    This document is valid and has no error -> Option A
  4. Quick Check:

    Quoted or unquoted field names both valid [OK]
Hint: MongoDB allows mixed quoted/unquoted field names [OK]
Common Mistakes:
  • Thinking all field names must be quoted
  • Assuming mixing quotes causes error
  • Believing values must be only numbers
5. You want to store a user's profile with name, age, and a list of addresses (each with street and city) in MongoDB. Which document structure correctly represents this?
hard
A. { name: 'Eva', age: 28, addresses: '1st Ave, NY; 2nd St, LA' }
B. { name: 'Eva', age: 28, addresses: [{ street: '1st Ave', city: 'NY' }, { street: '2nd St', city: 'LA' }] }
C. { name: 'Eva', age: 28, addresses: { street: '1st Ave', city: 'NY' }, { street: '2nd St', city: 'LA' } }
D. [ name: 'Eva', age: 28, addresses: [{ street: '1st Ave', city: 'NY' }] ]

Solution

  1. Step 1: Understand nested documents and arrays

    Addresses is a list (array) of objects (documents), each with street and city fields.
  2. Step 2: Check each option's structure

    { name: 'Eva', age: 28, addresses: [{ street: '1st Ave', city: 'NY' }, { street: '2nd St', city: 'LA' }] } correctly uses an array of documents inside the addresses field. Others use strings, invalid syntax, or wrong brackets.
  3. Final Answer:

    { name: 'Eva', age: 28, addresses: [{ street: '1st Ave', city: 'NY' }, { street: '2nd St', city: 'LA' }] } -> Option B
  4. Quick Check:

    Array of nested documents = { name: 'Eva', age: 28, addresses: [{ street: '1st Ave', city: 'NY' }, { street: '2nd St', city: 'LA' }] } [OK]
Hint: Use array of documents for multiple nested objects [OK]
Common Mistakes:
  • Storing nested data as a plain string
  • Using invalid JSON syntax with multiple objects outside array
  • Using square brackets for entire document instead of curly braces