0
0
MongoDBquery~30 mins

$in for matching a set in MongoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
$in Operator for Matching a Set in MongoDB
📖 Scenario: You are managing a small online bookstore database. You want to find books that belong to certain genres from a list of genres.
🎯 Goal: Build a MongoDB query using the $in operator to find all books whose genre matches any genre in a given set.
📋 What You'll Learn
Create a collection called books with specific book documents
Define a list of genres to search for
Write a MongoDB query using $in to find books matching those genres
Complete the query to return only the matching books
💡 Why This Matters
🌍 Real World
Using $in helps quickly find records matching any value from a list, common in filtering products, users, or categories.
💼 Career
Database developers and data analysts often use $in to write efficient queries for filtering data sets.
Progress0 / 4 steps
1
Create the books collection with sample data
Create a MongoDB collection called books and insert these exact documents: { title: "The Hobbit", genre: "Fantasy" }, { title: "1984", genre: "Dystopian" }, { title: "To Kill a Mockingbird", genre: "Classic" }, { title: "The Great Gatsby", genre: "Classic" }, { title: "Neuromancer", genre: "Science Fiction" }.
MongoDB
Need a hint?

Use db.books.insertMany([...]) with the exact documents listed.

2
Define the list of genres to search for
Create a variable called searchGenres and assign it the array ["Classic", "Science Fiction"].
MongoDB
Need a hint?

Use const searchGenres = ["Classic", "Science Fiction"] to define the array.

3
Write the MongoDB query using $in
Write a MongoDB query called query that uses { genre: { $in: searchGenres } } to find books whose genre is in the searchGenres array.
MongoDB
Need a hint?

Use const query = { genre: { $in: searchGenres } } to create the query.

4
Complete the query to find matching books
Use db.books.find(query) to find all books matching the genres in searchGenres.
MongoDB
Need a hint?

Use db.books.find(query) to retrieve the matching documents.