Bird
Raised Fist0
MongoDBquery~20 mins

Dot notation for embedded documents 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
🎖️
Dot Notation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Find the city of the first user
Given the collection users where each document has an embedded address field with a city subfield, what is the output of this query?

db.users.findOne({}, {"address.city": 1, _id: 0})
MongoDB
db.users.insertMany([
  {name: "Alice", address: {city: "New York", zip: "10001"}},
  {name: "Bob", address: {city: "Los Angeles", zip: "90001"}}
])
A{"address": "New York"}
B{"address": {"city": "New York"}}
C{"city": "New York"}
D{"address.city": "New York"}
Attempts:
2 left
💡 Hint
Remember that dot notation in projection returns the embedded document with only the specified field.
query_result
intermediate
2:00remaining
Retrieve nested field using dot notation
Consider a collection orders with documents like:
{
  _id: 1,
  customer: {name: "John", contact: {email: "john@example.com", phone: "123-456"}},
  total: 100
}

What does this query return?

db.orders.findOne({}, {"customer.contact.email": 1, _id: 0})
MongoDB
db.orders.insertOne({
  _id: 1,
  customer: {name: "John", contact: {email: "john@example.com", phone: "123-456"}},
  total: 100
})
A{"contact": {"email": "john@example.com"}}
B{"customer.contact.email": "john@example.com"}
C{"email": "john@example.com"}
D{"customer": {"contact": {"email": "john@example.com"}}}
Attempts:
2 left
💡 Hint
Projection with dot notation returns the full path of embedded documents with only the specified leaf field.
📝 Syntax
advanced
2:00remaining
Identify the invalid dot notation query
Which of the following MongoDB queries using dot notation will cause a syntax error or fail to run?
Adb.products.find({}, {"details.price": 1})
Bdb.products.find({}, {"details.price": 1, _id: 0})
Cdb.products.find({}, {details.price: 1, _id: 0})
D)}0 :di_ ,1 :"ecirp.sliated"{ ,}{(dnif.stcudorp.bd
Attempts:
2 left
💡 Hint
Check how keys with dots must be quoted in JavaScript objects.
optimization
advanced
2:00remaining
Optimize query to return only nested field values
You want to retrieve only the score field inside the embedded stats document from all documents in players. Which query is the most efficient to return only the score values without extra nesting?
Adb.players.aggregate([{ $project: { score: "$stats.score", _id: 0 } }])
Bdb.players.find({}, {score: "$stats.score", _id: 0})
Cdb.players.find({}, {score: 1, _id: 0})
Ddb.players.find({}, {"stats.score": 1, _id: 0})
Attempts:
2 left
💡 Hint
Projection with dot notation returns nested documents; aggregation can reshape fields.
🧠 Conceptual
expert
3:00remaining
Understanding dot notation behavior with arrays
Given a collection books with documents like:
{
  title: "Book A",
  authors: [
    {name: "Alice", age: 30},
    {name: "Bob", age: 40}
  ]
}

What does the query db.books.findOne({}, {"authors.name": 1, _id: 0}) return?
A{"authors": [{"name": "Alice"}, {"name": "Bob"}]}
B{"authors.name": ["Alice", "Bob"]}
C{"name": ["Alice", "Bob"]}
D{"authors": "Alice, Bob"}
Attempts:
2 left
💡 Hint
Projection with dot notation on arrays returns the array with only the specified fields in each element.

Practice

(1/5)
1. What does dot notation in MongoDB allow you to do with embedded documents?
easy
A. Access nested fields inside embedded documents
B. Create new collections automatically
C. Encrypt data within documents
D. Delete entire databases

Solution

  1. Step 1: Understand dot notation purpose

    Dot notation is used to reach inside nested or embedded documents to access specific fields.
  2. Step 2: Compare with other options

    Creating collections, encrypting data, or deleting databases are unrelated to dot notation.
  3. Final Answer:

    Access nested fields inside embedded documents -> Option A
  4. Quick Check:

    Dot notation = Access nested fields [OK]
Hint: Dot notation accesses nested fields using dots [OK]
Common Mistakes:
  • Thinking dot notation creates collections
  • Confusing dot notation with encryption
  • Assuming dot notation deletes data
2. Which of the following is the correct syntax to query the field address.city in MongoDB?
easy
A. { address.city: 'New York' }
B. { address->city: 'New York' }
C. { 'address.city': 'New York' }
D. { address[city]: 'New York' }

Solution

  1. Step 1: Understand dot notation syntax in queries

    Field names with dots must be quoted as a single string in MongoDB queries.
  2. Step 2: Evaluate each option

    { 'address.city': 'New York' } uses quotes correctly around 'address.city'. Options A, C, and D use invalid syntax.
  3. Final Answer:

    { 'address.city': 'New York' } -> Option C
  4. Quick Check:

    Quotes needed for dot field names = { 'address.city': 'New York' } [OK]
Hint: Quote dot notation keys in queries [OK]
Common Mistakes:
  • Not quoting dot notation keys
  • Using arrows or brackets instead of dots
  • Using unquoted keys with dots
3. Given the collection documents:
{ name: 'Alice', contact: { phone: '1234', email: 'alice@example.com' } }
What will the query db.collection.find({ 'contact.phone': '1234' }) return?
medium
A. Documents where contact.phone equals '1234'
B. Documents where contact.email equals '1234'
C. Documents where name equals '1234'
D. No documents, syntax error

Solution

  1. Step 1: Understand the query filter

    The query filters documents where the embedded field contact.phone equals '1234'.
  2. Step 2: Match with document data

    The example document has contact.phone as '1234', so it matches and will be returned.
  3. Final Answer:

    Documents where contact.phone equals '1234' -> Option A
  4. Quick Check:

    Dot notation filters embedded fields = Documents where contact.phone equals '1234' [OK]
Hint: Dot notation filters nested fields directly [OK]
Common Mistakes:
  • Confusing phone with email field
  • Thinking dot notation causes syntax error
  • Assuming it filters top-level fields only
4. What is wrong with this MongoDB query to update the city in an embedded address document?
db.users.updateOne({ name: 'Bob' }, { $set: { address.city: 'Boston' } })
medium
A. Update operator $set is incorrect
B. Field name with dot must be quoted as a string
C. Collection name should be 'user' not 'users'
D. Query filter is missing

Solution

  1. Step 1: Check update syntax for embedded fields

    When using dot notation in update keys, the field name must be quoted as a string.
  2. Step 2: Analyze the given query

    The query uses address.city without quotes, which causes a syntax error.
  3. Final Answer:

    Field name with dot must be quoted as a string -> Option B
  4. Quick Check:

    Quote dot notation keys in updates = Field name with dot must be quoted as a string [OK]
Hint: Quote dot notation keys in update documents [OK]
Common Mistakes:
  • Not quoting dot notation keys in $set
  • Misusing update operators
  • Assuming collection name is wrong
5. You have documents with nested structure:
{ _id: 1, profile: { name: 'Eve', contacts: { email: 'eve@mail.com', phone: '555' } } }
How do you write a query to find documents where the phone number is '555' using dot notation?
hard
A. { 'profile.contacts': { phone: '555' } }
B. { profile.contacts.phone: '555' }
C. { profile.contacts['phone']: '555' }
D. { 'profile.contacts.phone': '555' }

Solution

  1. Step 1: Identify the full path to the nested field

    The phone field is inside contacts, which is inside profile, so the path is profile.contacts.phone.
  2. Step 2: Use dot notation with quotes in query

    To query nested fields, use quotes around the full dot notation key: 'profile.contacts.phone'.
  3. Final Answer:

    { 'profile.contacts.phone': '555' } -> Option D
  4. Quick Check:

    Quote full dot notation path in query = { 'profile.contacts.phone': '555' } [OK]
Hint: Quote full dot notation path in queries [OK]
Common Mistakes:
  • Not quoting dot notation keys
  • Using object instead of dot notation in query
  • Using brackets inside dot notation keys