0
0
MongoDBquery~30 mins

Arrays in documents in MongoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
Working with Arrays in MongoDB Documents
📖 Scenario: You are managing a small library database. Each book document contains information about the book and a list of authors who contributed to it.
🎯 Goal: Create a MongoDB document for a book with an array of authors, add a new author to the array, and then query the document to find books with a specific author.
📋 What You'll Learn
Create a book document with an authors array containing exact author names
Add a new author to the authors array using an update operation
Query the collection to find books where the authors array contains a specific author
💡 Why This Matters
🌍 Real World
Managing collections of items with multiple related values, like books with multiple authors, products with multiple tags, or users with multiple roles.
💼 Career
Understanding how to work with arrays in MongoDB documents is essential for backend developers and database administrators who design and query NoSQL databases.
Progress0 / 4 steps
1
Create a book document with an authors array
Insert a document into the books collection with the following fields: title set to "Learning MongoDB", year set to 2023, and an authors array containing exactly these strings: "Alice Johnson" and "Bob Smith".
MongoDB
Need a hint?

Use insertOne to add a document with an authors array.

2
Add a new author to the authors array
Use the updateOne method on the books collection to add the author "Carol White" to the authors array of the book with title "Learning MongoDB". Use the $push operator.
MongoDB
Need a hint?

Use updateOne with a filter on title and the $push operator to add to the array.

3
Query books by author in the authors array
Write a query using find on the books collection to find all books where the authors array contains the author "Carol White".
MongoDB
Need a hint?

Use find with a filter on the authors array to match the author.

4
Complete the document with an _id field and verify structure
Ensure the book document includes the _id field automatically created by MongoDB and that the authors array contains exactly these three authors: "Alice Johnson", "Bob Smith", and "Carol White". The code should include the insert, update, and find operations as before.
MongoDB
Need a hint?

The _id field is added automatically by MongoDB when inserting a document.