Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

MongoDB Aggregation

MongoDB $eq Operator

Updated on Oct 31, 2024
By Mari Selvan
👁️ 40 - Views
⏳ 4 mins
💬 1 Comment
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:

syntax.js
Copied
Copy To Clipboard
{ $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:

Input
Copied
Copy To Clipboard
[
  { "_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:

example.js
Copied
Copy To Clipboard
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:

Output
Copied
Copy To Clipboard
{ "_id": ObjectId("609c26812e9274a86871bc6a"), "name": "Alice", "department": "Engineering", "salary": 60000 }

📚 Use Cases

  1. Data Filtering:

    The $eq operator is useful for selecting documents that match specific criteria, facilitating precise data retrieval and analysis.

  2. Conditional Aggregation:

    When performing complex aggregations, the $eq operator can be combined with other stages to filter documents based on equality conditions.

  3. 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:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

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

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy