0
0
MongoDBquery~30 mins

Array of embedded documents queries in MongoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
Querying Arrays of Embedded Documents in MongoDB
📖 Scenario: You are managing a small library database in MongoDB. Each book document contains an array of embedded documents representing reviews from readers. Each review has a reviewer name and a rating score.Your task is to write queries that find books based on conditions inside these embedded review documents.
🎯 Goal: Build MongoDB queries that filter books by conditions on the array of embedded reviews documents.
📋 What You'll Learn
Create a collection named books with documents containing an array field reviews of embedded documents
Add a configuration variable to specify a minimum rating threshold
Write a query to find books with at least one review having a rating greater than or equal to the threshold
Write a query to find books where a specific reviewer gave a rating below the threshold
💡 Why This Matters
🌍 Real World
Many real-world databases store related data as arrays of embedded documents, such as product reviews, comments, or order items. Knowing how to query these arrays is essential for retrieving meaningful information.
💼 Career
Database developers and data analysts often need to write queries that filter documents based on nested array data. Mastering $elemMatch and embedded document queries is a key skill for working with MongoDB.
Progress0 / 4 steps
1
Create the books collection with embedded reviews
Create a MongoDB collection named books and insert these two documents exactly:
{ title: "Learn MongoDB", reviews: [{ reviewer: "Alice", rating: 5 }, { reviewer: "Bob", rating: 3 }] }
{ title: "Mastering Databases", reviews: [{ reviewer: "Charlie", rating: 4 }, { reviewer: "Alice", rating: 2 }] }
MongoDB
Need a hint?

Use db.books.insertMany() with an array of two objects. Each object must have a title string and a reviews array of embedded documents with reviewer and rating.

2
Set a minimum rating threshold variable
Create a JavaScript variable named minRating and set it to 4 to use as the minimum rating threshold for queries.
MongoDB
Need a hint?

Use const minRating = 4 to create the variable.

3
Query books with any review rating >= minRating
Write a MongoDB query named highRatedBooks that finds all documents in books where the reviews array contains at least one embedded document with rating greater than or equal to minRating. Use the $elemMatch operator.
MongoDB
Need a hint?

Use db.books.find({ reviews: { $elemMatch: { rating: { $gte: minRating } } } }) and assign it to highRatedBooks.

4
Query books where Alice gave a rating below minRating
Write a MongoDB query named aliceLowRatings that finds all documents in books where the reviews array contains an embedded document with reviewer equal to "Alice" and rating less than minRating. Use the $elemMatch operator.
MongoDB
Need a hint?

Use db.books.find({ reviews: { $elemMatch: { reviewer: "Alice", rating: { $lt: minRating } } } }) and assign it to aliceLowRatings.