Filtering Products with Comparison Expressions in MongoDB Aggregation
📖 Scenario: You are managing a small online store's product database. You want to find products based on their price and stock availability using MongoDB aggregation queries.
🎯 Goal: Build a MongoDB aggregation pipeline that filters products using comparison expressions $eq and $gt to find products with specific price and stock conditions.
📋 What You'll Learn
Create a collection named products with given product documents
Add a variable to specify the minimum stock threshold
Write an aggregation pipeline using $match with $eq and $gt comparison expressions
Complete the pipeline to return only products that meet the price and stock criteria
💡 Why This Matters
🌍 Real World
Filtering products in an online store database to find items matching specific price and stock conditions.
💼 Career
Understanding MongoDB aggregation with comparison expressions is essential for backend developers and data analysts working with NoSQL databases.
Use db.products.insertMany([...]) with the exact product documents.
2
Set a minimum stock threshold variable
Create a variable called minStock and set it to 20 to use as the minimum stock threshold for filtering products.
MongoDB
Hint
Use const minStock = 20 to define the threshold.
3
Write an aggregation pipeline using $eq and $gt
Create a variable called pipeline that contains an aggregation pipeline with a $match stage. Use $eq to filter products where price equals 15 and $gt to filter products where stock is greater than minStock.
MongoDB
Hint
Use { $match: { price: { $eq: 15 }, stock: { $gt: minStock } } } inside the pipeline array.
4
Complete the aggregation query to find matching products
Use db.products.aggregate(pipeline) to run the aggregation pipeline and assign the result to a variable called result.
MongoDB
Hint
Use const result = db.products.aggregate(pipeline) to run the query.
Practice
(1/5)
1. What does the $eq operator do in a MongoDB aggregation pipeline?
easy
A. Checks if one value is greater than another
B. Sorts documents in ascending order
C. Checks if two values are equal
D. Filters documents by existence of a field
Solution
Step 1: Understand the purpose of $eq
The $eq operator compares two values to see if they are exactly the same.
Step 2: Compare with other operators
Unlike $gt which checks if one value is greater, $eq checks equality.
Final Answer:
Checks if two values are equal -> Option C
Quick Check:
$eq means equal [OK]
Hint: Remember: eq means equal, gt means greater [OK]
Common Mistakes:
Confusing $eq with $gt
Thinking $eq sorts data
Using $eq to check field existence
2. Which of the following is the correct syntax to use $gt in a MongoDB aggregation $match stage to find documents where age is greater than 30?
easy
A. { $match: { $gt: { age: 30 } } }
B. { $match: { age: { $gt: 30 } } }
C. { $match: { age: $gt(30) } }
D. { $match: { age: { gt: 30 } } }
Solution
Step 1: Recall correct operator syntax
MongoDB operators like $gt are used as keys inside an object with the field name as the key.
Step 2: Match correct structure
The correct syntax is { field: { $gt: value } }, so { age: { $gt: 30 } } is correct.
Hint: Operators go inside field object with $ prefix [OK]
Common Mistakes:
Placing $gt outside the field object
Using parentheses like a function
Missing $ before gt
3. Given the aggregation pipeline stage:
{ $match: { score: { $gt: 80 } } }
Which documents will be matched if the collection has documents with scores: 75, 80, 85, 90?
medium
A. Documents with scores 75, 85, and 90
B. Documents with scores 75 and 80
C. Documents with scores 80, 85, and 90
D. Documents with scores 85 and 90
Solution
Step 1: Understand $gt meaning
The $gt operator matches values strictly greater than the given number, here 80.
Step 2: Check which scores are greater than 80
Scores 85 and 90 are greater than 80; 75 and 80 are not.
Final Answer:
Documents with scores 85 and 90 -> Option D
Quick Check:
Only scores > 80 match = Documents with scores 85 and 90 [OK]
Hint: Remember $gt excludes equal values [OK]
Common Mistakes:
Including values equal to 80
Confusing $gt with $gte
Selecting lower values by mistake
4. Identify the error in this aggregation $match stage:
{ $match: { $eq: ["$status", "active"] } }
medium
A. Incorrect use of $eq outside a field key
B. Missing $gt operator
C. Wrong array syntax for $eq
D. No error, syntax is correct
Solution
Step 1: Check $eq usage in $match
The $eq operator is usually used inside $expr or aggregation expressions, not directly as a key in $match.
Step 2: Understand correct placement
To compare fields with $eq, it must be inside $expr, like { $match: { $expr: { $eq: ["$status", "active"] } } }.
Final Answer:
Incorrect use of $eq outside a field key -> Option A
Quick Check:
$eq needs $expr for expressions [OK]
Hint: Use $expr to compare fields with $eq [OK]
Common Mistakes:
Using $eq directly in $match without $expr
Confusing $eq array syntax
Assuming $eq works like a filter key
5. You want to filter documents where price is greater than 100 and category equals "electronics" using aggregation. Which $match stage correctly uses $gt and $eq together?