Database
NoSQL
MongoDB
Operators
Comparison Query Operators

Comparison Query Operators in MongoDB

Introduction

Comparison Query Operators in MongoDB allow you to filter documents based on specified criteria by comparing field values against given conditions. These operators are essential for querying data effectively, providing powerful ways to match, filter, and sort data in your MongoDB collections.

Common Comparison Operators

1. $gt (Greater Than)

Description: This operator selects documents where the field value is greater than the specified value.

Use Case: Useful when you want to find entries above a certain threshold, like finding products priced higher than a specific amount.

db.products.find({ price: { $gt: 500 } });

2. $lt (Less Than)

Description: This operator selects documents where the field value is less than the specified value.

Use Case: Ideal for finding entries below a particular threshold, such as products with a stock level below a set limit.

db.products.find({ stock: { $lt: 100 } });

3. $eq (Equal)

Description: Selects documents where the field value matches the specified value exactly.

Use Case: Use this to find documents that match an exact criterion, such as locating items with a specific rating.

db.products.find({ rating: { $eq: 4 } });

4. $ne (Not Equal)

Description: Selects documents where the field value does not match the specified value.

Use Case: Great for filtering out specific entries, like finding products not matching a particular category.

db.products.find({ category: { $ne: "Electronics" } });

5. $gte (Greater Than or Equal)

Description: This operator finds documents where the field value is greater than or equal to the specified value.

Use Case: Useful for locating entries that meet or exceed a threshold, such as products that cost at least a certain amount.

db.products.find({ price: { $gte: 1000 } });

6. $lte (Less Than or Equal)

Description: Selects documents where the field value is less than or equal to the specified value.

Use Case: Ideal for finding products that are priced within a certain limit or below.

db.products.find({ price: { $lte: 300 } });

7. $in

Description: Matches documents where the field value is within an array of specified values.

Use Case: Use this to find entries that belong to multiple specified categories, such as products that fit within multiple classifications.

db.products.find({ category: { $in: ["Electronics", "Furniture"] } });

8. $nin (Not In)

Description: Selects documents where the field value is not present within an array of specified values.

Use Case: Useful for excluding certain categories or values, like finding products that do not belong to certain brands.

db.products.find({ category: { $nin: ["Clothing", "Toys"] } });

9. $not

Description: Inverts the effect of a comparison or other query operator.

Use Case: Helpful when you need to negate conditions, such as finding values that do not meet a set of specified rules.

db.products.find({ price: { $not: { $gt: 500 } } });

10. $all

Description: Matches arrays that contain all elements specified in the query.

Use Case: Useful for matching documents where an array field includes all specified values.

db.orders.find({ items: { $all: ["Apple", "Banana"] } });

11. $exists

Description: Checks if a field exists or not within the documents.

Use Case: Used to filter documents based on the presence or absence of a particular field.

db.products.find({ discount: { $exists: true } });

Conclusion

Comparison operators in MongoDB are essential for crafting specific queries that meet the needs of your application. By mastering these operators, you can filter data efficiently, ensuring that your MongoDB queries return exactly the results you need.