0
0
MongoDBquery~30 mins

$elemMatch for complex array queries in MongoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
$elemMatch for complex array queries
📖 Scenario: You are managing a database for a library. Each book document contains an array of reviews. Each review has a rating and a comment. You want to find books that have at least one review with a rating of 5 and a comment containing the word "excellent".
🎯 Goal: Build a MongoDB query using $elemMatch to find books where the reviews array contains at least one review with rating equal to 5 and comment containing "excellent".
📋 What You'll Learn
Create a collection called books with documents containing a title and an array called reviews.
Each review in the reviews array must have a rating (number) and a comment (string).
Write a query using $elemMatch to find books with at least one review where rating is 5 and comment contains the word "excellent".
💡 Why This Matters
🌍 Real World
Libraries, e-commerce sites, and review platforms often store multiple reviews or comments inside an array field. Using <code>$elemMatch</code> helps find documents where at least one array element meets complex conditions.
💼 Career
Understanding <code>$elemMatch</code> is essential for database developers and backend engineers working with MongoDB to write efficient and precise queries for nested array data.
Progress0 / 4 steps
1
Create the books collection with sample documents
Create a variable called books and assign it an array with two documents. The first document should have title set to "MongoDB Basics" and reviews as an array with one review: { rating: 5, comment: "An excellent introduction" }. The second document should have title set to "Advanced MongoDB" and reviews as an array with one review: { rating: 4, comment: "Good but not excellent" }.
MongoDB
Need a hint?

Use an array of objects. Each object has title and reviews keys. reviews is an array of objects with rating and comment.

2
Create a query filter using $elemMatch
Create a variable called query and assign it an object that uses $elemMatch on the reviews field. The $elemMatch should look for a review with rating equal to 5 and comment containing the substring "excellent" using a regular expression.
MongoDB
Need a hint?

Use $elemMatch with an object that has rating: 5 and comment with a regular expression to match "excellent" case-insensitively.

3
Apply the query to filter the books array
Create a variable called matchingBooks and assign it the result of filtering the books array using the query variable. Use the Array.filter() method and inside the callback, use Array.some() on the reviews array to check if any review matches the rating and comment conditions from query.
MongoDB
Need a hint?

Use filter on books and inside use some on reviews. Compare rating and test comment with the regex.

4
Complete the query by exporting the matching books
Add a line to export the matchingBooks variable using module.exports so it can be used in other files.
MongoDB
Need a hint?

Use module.exports = { matchingBooks } to export the variable.