MongoDB Query Operators

Powerful tools for filtering and finding documents

🔍 What are Query Operators?

Query operators help you find specific documents in MongoDB collections. They let you filter data using conditions like equals, greater than, or contains, making searches precise and powerful.


// Find users older than 25
db.users.find({ age: { $gt: 25 } })
                                    

Comparison Operators

Comparison operators let you compare values in your queries. They work with numbers, strings, and dates to filter documents based on conditions.

🟰

$eq (Equal)

Matches values equal to specified value

db.products.find({ price: { $eq: 100 } })

$ne (Not Equal)

Matches values not equal to specified value

db.products.find({ status: { $ne: "sold" } })
>

$gt (Greater Than)

Matches values greater than specified value

db.users.find({ age: { $gt: 18 } })
<

$lt (Less Than)

Matches values less than specified value

db.products.find({ price: { $lt: 50 } })

🔹 Logical Operators

Logical operators combine multiple conditions in your queries. Use them to create complex filters with AND, OR, and NOT logic.

🔸 $and Operator

// Find products that are cheap AND in stock
db.products.find({
    $and: [
        { price: { $lt: 100 } },
        { inStock: true }
    ]
})

Result:

{ "_id": 1, "name": "Keyboard", "price": 45, "inStock": true }
{ "_id": 2, "name": "Mouse", "price": 25, "inStock": true }

🔸 $or Operator

// Find users from USA OR Canada
db.users.find({
    $or: [
        { country: "USA" },
        { country: "Canada" }
    ]
})

Result:

{ "_id": 1, "name": "John", "country": "USA" }
{ "_id": 2, "name": "Sarah", "country": "Canada" }

🔸 $not Operator

// Find products NOT expensive
db.products.find({
    price: { $not: { $gt: 100 } }
})

🔹 Element Operators

Element operators check if fields exist or match specific types in your documents.

🔸 $exists Operator

// Find users who have email field
db.users.find({ email: { $exists: true } })

// Find users without phone number
db.users.find({ phone: { $exists: false } })

Result:

{ "_id": 1, "name": "Alice", "email": "[email protected]" }
{ "_id": 2, "name": "Bob", "email": "[email protected]" }

🔸 $type Operator

// Find documents where age is a number
db.users.find({ age: { $type: "number" } })

// Find documents where tags is an array
db.posts.find({ tags: { $type: "array" } })

🔹 Array Operators

Array operators help you query documents with array fields, checking for specific values or array sizes.

🔸 $in Operator

// Find products in specific categories
db.products.find({
    category: { $in: ["Electronics", "Books", "Toys"] }
})

Result:

{ "_id": 1, "name": "Laptop", "category": "Electronics" }
{ "_id": 2, "name": "Novel", "category": "Books" }

🔸 $all Operator

// Find posts with all specified tags
db.posts.find({
    tags: { $all: ["mongodb", "database"] }
})

🔸 $size Operator

// Find users with exactly 3 hobbies
db.users.find({
    hobbies: { $size: 3 }
})

🔹 String Operators

Search text fields using pattern matching with regular expressions.

🔸 $regex Operator

// Find users whose name starts with "J"
db.users.find({
    name: { $regex: /^J/, $options: "i" }
})

// Find products containing "phone" (case-insensitive)
db.products.find({
    name: { $regex: /phone/i }
})

Result:

{ "_id": 1, "name": "iPhone 15" }
{ "_id": 2, "name": "Samsung Phone" }

🔹 Practical Examples

Combine multiple operators for powerful queries:

Example 1: E-commerce Product Search

// Find affordable electronics in stock
db.products.find({
    $and: [
        { category: "Electronics" },
        { price: { $gte: 50, $lte: 500 } },
        { inStock: true },
        { rating: { $gte: 4.0 } }
    ]
})

Example 2: User Profile Search

// Find active users from specific countries
db.users.find({
    $and: [
        { status: "active" },
        { country: { $in: ["USA", "UK", "Canada"] } },
        { age: { $gte: 18, $lte: 65 } },
        { email: { $exists: true } }
    ]
})

🧠 Test Your Knowledge

Which operator finds values greater than a specified number?