Querying Array Elements Directly in MongoDB
📖 Scenario: You work at a bookstore that stores book information in a MongoDB collection. Each book document has a title and an array of reviews. Each review has a reviewer's name and a rating.You want to find books that have at least one review with a rating of 5.
🎯 Goal: Build a MongoDB query that finds books with at least one review having a rating of 5 by querying array elements directly.
📋 What You'll Learn
Create a collection named
books with documents containing title and reviews arrayAdd a configuration variable
targetRating set to 5Write a query that finds books where any review's
rating equals targetRatingComplete the query to return only the
title and matching reviews elements💡 Why This Matters
🌍 Real World
Many applications store lists of related data inside documents. Querying array elements directly helps find specific items quickly.
💼 Career
Database developers and backend engineers often write queries to filter nested array data in MongoDB for efficient data retrieval.
Progress0 / 4 steps