MongoDB Query Fundamentals
In MongoDB, queries are built using a filter document that specifies conditions. The find() method returns all documents matching those conditions. Unlike SQL's WHERE clause, MongoDB queries use operators (prefixed with $) to express complex logic.
In this lesson, you'll query the FreshBasket e-commerce database and learn to use comparison operators, logical operators, array operators, projections, and pagination. By the end, you'll be able to retrieve exactly the data you need.
Comparison Operators
Comparison operators allow you to filter documents based on numerical or value ranges. Here are the most common ones:
$gt — Greater than (>). Returns docs where field value is strictly greater.
$gte — Greater than or equal (>=). Includes the boundary value.
$lt — Less than (<). Returns docs where field value is strictly smaller.
$lte — Less than or equal (<=). Includes the boundary value.
$ne — Not equal (!=). Returns docs where field does not match the value.
Q1: Find Products Over a Certain Price
Let's find all products in the products collection with a price greater than $10:
{ _id: ObjectId(...), name: "Greek Yogurt", category: "Dairy", price: 8.99, stock: 30 } — filtered out (not > 10)
Q2: Find Products Within a Price Range
Combine multiple comparison operators to find products priced between $5 and $15:
{ _id: ObjectId(...), name: "Greek Yogurt", category: "Dairy", price: 8.99, stock: 30 }
Q3: Find Products with Low Stock
Find all products with stock less than 20 units:
Logical Operators
Logical operators allow you to combine multiple conditions. The most important is implicit AND — when you specify multiple fields in a filter, MongoDB requires all conditions to be true.
Implicit AND
Specify multiple conditions in the same filter object. All must be true for a document to match:
Explicit AND with $and
Use $and when you need to combine the same field with multiple operators or for clarity:
Q4: OR Operator
Use $or when at least one condition should be true:
Q5: IN Operator
Use $in to match any value in a list. This is cleaner than multiple $or conditions:
Q6: NOT IN Operator
Use $nin (not in) to exclude certain values:
Q7: NOT Operator
Use $not to negate a condition:
Element & Array Operators
MongoDB documents often contain arrays. These operators help you query array fields and check for field existence.
Q8: $exists Operator
Check whether a field exists in a document:
Q9: $type Operator
Match documents where a field has a specific data type:
Q10: $elemMatch Operator
Query array elements with multiple conditions. Perfect for arrays of objects:
Q11: $size Operator
Match arrays with a specific length:
Projections — Select Specific Fields
Projections control which fields are included or excluded from query results. This is useful for reducing network traffic and focusing on relevant data. The second parameter of find() is the projection document.
Q12: Include Specific Fields
Use 1 to include fields. Return only name and price:
{ _id: ObjectId(...), name: "Greek Yogurt", price: 8.99 }
Note: _id is included by default
Q13: Exclude Specific Fields
Use 0 to exclude fields. Hide the stock field:
Q14: Exclude _id Field
The _id field is always included unless you explicitly exclude it: