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
Dot notation for embedded documents
📖 Scenario: You are managing a small library database. Each book has a title and an embedded document for the author details, including the author's name and birth year.
🎯 Goal: You will create a collection of books with embedded author documents. Then, you will write queries using dot notation to access the author's name and birth year.
📋 What You'll Learn
Create a collection called books with three documents.
Each document must have a title field and an embedded author document with name and birthYear fields.
Create a variable called authorNameField that stores the string for accessing the author's name using dot notation.
Write a query that finds all books and projects only the title and the author's name using the authorNameField variable.
Write a query that finds all books and projects only the title and the author's birth year using dot notation directly.
💡 Why This Matters
🌍 Real World
Embedded documents are common in MongoDB to store related data together, like author details inside a book document.
💼 Career
Understanding dot notation helps you query and manipulate nested data efficiently in NoSQL databases like MongoDB, a valuable skill for backend and database developers.
Progress0 / 4 steps
1
Create the books collection with embedded author documents
Create a variable called books and assign it an array of three documents. Each document should have a title string and an author embedded document with name and birthYear fields exactly as follows: { title: "Book One", author: { name: "Alice", birthYear: 1970 } }, { title: "Book Two", author: { name: "Bob", birthYear: 1980 } }, and { title: "Book Three", author: { name: "Charlie", birthYear: 1990 } }.
MongoDB
Hint
Use an array of objects. Each object has a title and an author object with name and birthYear.
2
Create a variable for the author's name field using dot notation
Create a variable called authorNameField and assign it the string "author.name" to represent the dot notation path to the author's name.
MongoDB
Hint
Assign the string "author.name" to the variable authorNameField.
3
Write a query to find all books projecting title and author's name using the variable
Write a query called queryAuthorNames that uses books.find() with an empty filter {} and a projection object that includes title: 1 and uses the authorNameField variable as a key with value 1 to project the author's name.
MongoDB
Hint
Use bracket notation [authorNameField] to use the variable as a key in the projection object.
4
Write a query to find all books projecting title and author's birth year using dot notation
Write a query called queryAuthorBirthYears that uses books.find() with an empty filter {} and a projection object that includes title: 1 and "author.birthYear": 1 to project the author's birth year using dot notation directly.
MongoDB
Hint
Use the string "author.birthYear" directly as a key in the projection object.
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
Step 1: Understand dot notation purpose
Dot notation is used to reach inside nested or embedded documents to access specific fields.
Step 2: Compare with other options
Creating collections, encrypting data, or deleting databases are unrelated to dot notation.
Final Answer:
Access nested fields inside embedded documents -> Option A
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
Step 1: Understand dot notation syntax in queries
Field names with dots must be quoted as a single string in MongoDB queries.
Step 2: Evaluate each option
{ 'address.city': 'New York' } uses quotes correctly around 'address.city'. Options A, C, and D use invalid syntax.
Final Answer:
{ 'address.city': 'New York' } -> Option C
Quick Check:
Quotes needed for dot field names = { 'address.city': 'New York' } [OK]