Bird
Raised Fist0
MongoDBquery~10 mins

Dot notation for embedded documents in MongoDB - Step-by-Step Execution

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
Concept Flow - Dot notation for embedded documents
Start with collection
Identify embedded document
Use dot notation: 'parent.child'
Query or update using dot notation
Return or modify embedded field value
Dot notation lets you access or change fields inside embedded documents by writing 'parent.child'.
Execution Sample
MongoDB
db.users.find({"address.city": "Boston"})
Finds users whose embedded address document has city equal to Boston.
Execution Table
StepActionQuery PartEvaluationResult
1Start query on 'users' collectionAll user documents available
2Look inside each document's 'address' fieldaddressCheck if 'address' exists and is a documentAccess embedded document
3Access 'city' inside 'address' using dot notationaddress.cityCompare city value to 'Boston'True or False per document
4Return documents where city is 'Boston'address.city = 'Boston'Filter appliedDocuments matching condition
5End queryResult set returned
💡 Query ends after filtering all documents by embedded field 'address.city' equals 'Boston'
Variable Tracker
VariableStartAfter Step 2After Step 3Final
current_documentFull user docEmbedded 'address' doc extracted'city' field value extractedDocument included or excluded based on city
Key Moments - 2 Insights
Why do we write 'address.city' instead of just 'city'?
Because 'city' is inside the embedded 'address' document, so dot notation 'address.city' tells MongoDB to look inside 'address' for 'city' (see execution_table step 3).
What happens if a document has no 'address' field?
The query skips that document because 'address.city' cannot be found, so the condition is false (see execution_table step 2 and 3).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step do we check the 'city' value inside 'address'?
AStep 2
BStep 4
CStep 3
DStep 1
💡 Hint
Check the 'Query Part' column for 'address.city' in execution_table rows.
According to variable_tracker, what is the value of 'current_document' after Step 3?
AFull user document
B'city' field value extracted
COnly the 'address' embedded document
DFiltered result set
💡 Hint
Look at the 'After Step 3' column for 'current_document' in variable_tracker.
If a user document has no 'address' field, what will happen during the query?
AIt will be skipped (not matched)
BIt will cause an error
CIt will match the query
DIt will update the document
💡 Hint
Refer to key_moments about missing 'address' field and execution_table steps 2 and 3.
Concept Snapshot
Dot notation accesses fields inside embedded documents.
Syntax: 'parent.child' (e.g., 'address.city').
Used in queries and updates to target nested fields.
If embedded document missing, condition is false.
Helps filter or modify nested data easily.
Full Transcript
Dot notation in MongoDB lets you access or update fields inside embedded documents by writing the parent field name, a dot, then the child field name. For example, to find users living in Boston, you write a query with 'address.city' equal to 'Boston'. The database looks inside each user's 'address' embedded document and checks the 'city' field. If the city matches, the document is returned. If a document has no 'address' field, it is skipped because the condition cannot be met. This way, dot notation helps you work with nested data easily and precisely.

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