MongoDB Aggregation
MongoDB $eq Operator
Photo Credit to CodeToFun
🙋 Introduction
In MongoDB's aggregation framework, the $eq
operator plays a fundamental role in filtering documents based on equality conditions. This operator allows users to select documents where a specified field matches a certain value, enabling precise data retrieval and analysis.
Let's delve into how the $eq
operator functions within MongoDB's aggregation pipelines and explore its practical applications.
💡 Syntax
The syntax for the $eq
method is straightforward:
{ $eq: [ <field>, <value> ] }
- $eq: This operator signifies that the subsequent operation will check for equality.
- <field>: This represents the field to be compared.
- <value>: This represents the value to compare against.
📝 Example
⌨️ Input
Consider a collection named employees containing documents with fields name, department, and salary. Here are sample documents from the employees collection:
[
{ "_id": ObjectId("609c26812e9274a86871bc6a"), "name": "Alice", "department": "Engineering", "salary": 60000 },
{ "_id": ObjectId("609c26812e9274a86871bc6b"), "name": "Bob", "department": "Sales", "salary": 50000 },
{ "_id": ObjectId("609c26812e9274a86871bc6c"), "name": "Charlie", "department": "Engineering", "salary": 65000 }
]
🔄 Aggregation
Suppose we want to find employees with a salary of $60,000. Here's how you can achieve this using the $eq
operator:
db.employees.aggregate([
{
$match: {
salary: { $eq: 60000 }
}
}
])
🧩 Explanation
- $match: This stage filters documents based on the specified condition.
- $eq: Checks if the salary field equals the specified value (60000).
When discussing how the above aggregation works:
- Only one document matches the condition where "salary" equals 60000.
- The output includes the document with the name "Alice", belonging to the Engineering department, and having a salary of 60000.
- Documents for "Bob" and "Charlie" are excluded because their salaries do not match the specified condition.
💻 Output
Now, let's take a look at the output generated by the aggregation pipeline:
{ "_id": ObjectId("609c26812e9274a86871bc6a"), "name": "Alice", "department": "Engineering", "salary": 60000 }
📚 Use Cases
Data Filtering:
The
$eq
operator is useful for selecting documents that match specific criteria, facilitating precise data retrieval and analysis.Conditional Aggregation:
When performing complex aggregations, the
$eq
operator can be combined with other stages to filter documents based on equality conditions.Data Validation:
In applications where data integrity is crucial, the
$eq
operator can be used to validate that certain fields match expected values.
🎉 Conclusion
The $eq
operator in MongoDB's aggregation framework provides a simple yet powerful mechanism for filtering documents based on equality conditions. Whether you're retrieving specific records, validating data, or conducting conditional aggregations, mastering the usage of $eq
empowers you to efficiently query and analyze your MongoDB collections.
With its intuitive syntax and diverse applications, the $eq
operator proves to be an invaluable asset for handling data effectively within MongoDB. Incorporate it into your aggregation pipelines to streamline data retrieval processes and gain deeper insights into your datasets.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (MongoDB $eq Operator), please comment here. I will help you immediately.