0
0
MongoDBquery~30 mins

Combining comparison operators in MongoDB - Mini Project: Build & Apply

Choose your learning style9 modes available
Combining Comparison Operators in MongoDB Queries
📖 Scenario: You are managing a small online bookstore database. You want to find books that meet specific price and rating criteria to recommend to customers.
🎯 Goal: Build a MongoDB query that uses combined comparison operators to find books priced between $10 and $30 and with a rating greater than 4.
📋 What You'll Learn
Create a collection named books with sample book documents
Add a variable for the price range limits
Write a MongoDB query using $gte, $lte, and $gt operators combined with $and
Complete the query to find books matching the criteria
💡 Why This Matters
🌍 Real World
Filtering products or items in a database based on multiple conditions is common in online stores, inventory systems, and recommendation engines.
💼 Career
Understanding how to combine comparison operators in queries is essential for database querying roles, backend development, and data analysis.
Progress0 / 4 steps
1
Create the books collection with sample data
Create a variable called books and assign it a list of three book documents. Each document should have these exact fields and values: { title: "Book A", price: 15, rating: 4.5 }, { title: "Book B", price: 25, rating: 3.9 }, and { title: "Book C", price: 35, rating: 4.8 }.
MongoDB
Need a hint?

Use a list with three dictionaries, each representing a book with the exact fields and values.

2
Define the price range limits
Create two variables called min_price and max_price and set them to 10 and 30 respectively.
MongoDB
Need a hint?

Set min_price to 10 and max_price to 30 as numbers.

3
Write the MongoDB query with combined comparison operators
Create a variable called query and assign it a MongoDB query object that uses $and to combine these conditions: price greater than or equal to min_price, price less than or equal to max_price, and rating greater than 4. Use $gte, $lte, and $gt operators exactly as shown.
MongoDB
Need a hint?

Use $and with a list of conditions combining $gte, $lte, and $gt for the fields.

4
Complete the query to find matching books
Create a variable called matching_books and assign it the result of filtering the books list using the query conditions. Use a list comprehension that checks each book's price and rating against the query criteria exactly as defined.
MongoDB
Need a hint?

Use a list comprehension to filter books matching all query conditions.