0
0
MongoDBquery~30 mins

Insert with nested documents in MongoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
Insert with Nested Documents in MongoDB
📖 Scenario: You are managing a small library database. Each book has a title, author, and details about its publisher. The publisher information includes the name and address, which is a nested document.
🎯 Goal: Create a MongoDB document for a book that includes nested publisher details, and insert it into the books collection.
📋 What You'll Learn
Create a document with fields title, author, and publisher
The publisher field must be a nested document with name and address
Insert the document into the books collection
💡 Why This Matters
🌍 Real World
Nested documents are common in MongoDB to represent related data in one place, like a book and its publisher.
💼 Career
Understanding nested documents and insert operations is essential for working with MongoDB in real-world applications.
Progress0 / 4 steps
1
Create the book document
Create a variable called book that holds a document with title set to "The Great Gatsby" and author set to "F. Scott Fitzgerald".
MongoDB
Need a hint?

Use curly braces to create a document. Add title and author as keys with their values.

2
Add nested publisher document
Add a publisher field to the book document. This field should be a nested document with name set to "Scribner" and address set to "123 Publisher St".
MongoDB
Need a hint?

Inside the book dictionary, add a key publisher with another dictionary as its value.

3
Insert the document into the collection
Use db.books.insertOne(book) to insert the book document into the books collection.
MongoDB
Need a hint?

Use the insertOne method on the books collection to add the document.

4
Verify the insertion with a query
Write a query using db.books.findOne({"title": "The Great Gatsby"}) to retrieve the inserted document.
MongoDB
Need a hint?

Use findOne with a filter on title to get the document.