0
0
MongoDBquery~30 mins

One-to-one embedding pattern in MongoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
One-to-One Embedding Pattern in MongoDB
📖 Scenario: You are building a simple database for a small library. Each book has exactly one author, and you want to store the author details inside the book document itself to keep related data together.
🎯 Goal: Create a MongoDB collection called books where each book document embeds exactly one author document inside it. You will first create the book documents, then add author details as embedded documents, and finally query the collection to retrieve books with their authors.
📋 What You'll Learn
Create a books collection with at least two book documents containing only the book title and ISBN.
Add an embedded author document inside each book document with fields name and birthYear.
Write a query to find all books and show their embedded author details.
Use the one-to-one embedding pattern to keep author data inside the book document.
💡 Why This Matters
🌍 Real World
Many applications store related data together in one document to simplify data access and improve performance, especially when the relationship is one-to-one.
💼 Career
Understanding embedding patterns in MongoDB is essential for designing efficient NoSQL databases used in web development, data engineering, and backend services.
Progress0 / 4 steps
1
Create the initial books collection with book documents
Insert two documents into the books collection with these exact fields and values: { title: "The Great Gatsby", isbn: "9780743273565" } and { title: "1984", isbn: "9780451524935" }.
MongoDB
Need a hint?

Use insertMany to add multiple documents to the books collection.

2
Add embedded author documents inside each book
Update the books collection to embed an author document inside each book. For "The Great Gatsby", add { name: "F. Scott Fitzgerald", birthYear: 1896 }. For "1984", add { name: "George Orwell", birthYear: 1903 }.
MongoDB
Need a hint?

Use updateOne with $set to add the author field inside each book document.

3
Query all books with their embedded authors
Write a query to find all documents in the books collection and return the title, isbn, and embedded author fields.
MongoDB
Need a hint?

Use find with a projection to show only the title, isbn, and author fields.

4
Complete the one-to-one embedded document pattern
Ensure the books collection documents have the embedded author field inside each book document, confirming the one-to-one embedding pattern is correctly applied.
MongoDB
Need a hint?

Check that each book document has the author embedded document inside it.