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
Create and Query Embedded Documents in MongoDB
📖 Scenario: You are building a small database for a bookstore. Each book has details like title, author, and price. Additionally, each book has an embedded document for the publisher information, which includes the publisher's name and address.
🎯 Goal: Create a MongoDB collection with embedded documents for publisher details. Then, write a query to find all books published by a specific publisher.
📋 What You'll Learn
Create a collection called books with documents containing title, author, price, and an embedded document publisher with name and address fields.
Add a variable targetPublisher to store the publisher name to search for.
Write a query to find all books where the embedded publisher.name matches targetPublisher.
Complete the query by projecting only the title and author fields in the output.
💡 Why This Matters
🌍 Real World
Many real-world databases store related information inside embedded documents to keep data organized and easy to access, like storing publisher details inside book records.
💼 Career
Understanding embedded documents and querying nested fields is essential for working with MongoDB and other NoSQL databases, a common skill in backend development and data engineering.
Progress0 / 4 steps
1
Create the books collection with embedded publisher documents
Create a variable called books and assign it an array with these two documents exactly: 1. { title: "The Great Gatsby", author: "F. Scott Fitzgerald", price: 10.99, publisher: { name: "Scribner", address: "New York" } } 2. { title: "1984", author: "George Orwell", price: 8.99, publisher: { name: "Secker & Warburg", address: "London" } }
MongoDB
Hint
Use an array of objects. Each book object has a publisher object inside it.
2
Add a variable to hold the target publisher name
Create a variable called targetPublisher and set it to the string "Scribner".
MongoDB
Hint
Use const targetPublisher = "Scribner"; to store the publisher name.
3
Write a query to find books by the target publisher
Write a variable called query that uses books.filter() to find all books where book.publisher.name equals targetPublisher.
MongoDB
Hint
Use filter with a function checking book.publisher.name === targetPublisher.
4
Project only the title and author fields in the query result
Create a variable called result that maps over query to return objects with only title and author fields.
MongoDB
Hint
Use map to create new objects with only title and author.
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
Step 1: Understand MongoDB document structure
MongoDB stores data in documents, which can contain nested objects called embedded documents.
Step 2: Identify embedded document meaning
An embedded document is a document inside another document, not a separate collection or file.
Final Answer:
A document stored inside another document as a nested object -> Option A
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
Step 1: Review MongoDB insertOne syntax
insertOne takes a single document object with fields and values, including nested objects.
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.
Final Answer:
db.collection.insertOne({name: 'Alice', address: {city: 'NY', zip: 10001}}) -> Option D
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
Step 1: Understand dot notation in queries
MongoDB uses dot notation to query fields inside embedded documents.
Step 2: Analyze the query
The query looks for documents where the embedded field contact.email matches the given value.
Final Answer:
All documents where contact.email equals 'bob@example.com' -> Option B
To store multiple related items, use an array of embedded documents for flexibility and clarity.
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.