Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

MongoDB Aggregation

MongoDB $mod Operator

Updated on Oct 31, 2024
By Mari Selvan
👁️ 15 - Views
⏳ 4 mins
💬 1 Comment
MongoDB $mod Operator

Photo Credit to CodeToFun

🙋 Introduction

In MongoDB's aggregation framework, the $mod operator stands as a versatile tool for performing modulo operations on numerical data. This operator enables users to compute the remainder of dividing one number by another, opening doors to various applications in data manipulation and analysis.

Let's explore how the $mod operator can be effectively utilized within MongoDB's aggregation pipelines.

💡 Syntax

The syntax for the $mod method is straightforward:

syntax.js
Copied
Copy To Clipboard
{ $mod: [ <dividend>, <divisor> ] }
  • $mod: This operator signifies that the subsequent operation will perform a modulo operation.
  • <dividend>: This represents the number to be divided.
  • <divisor>: This represents the number by which the dividend will be divided.

📝 Example

⌨️ Input

Consider a collection named numbers containing documents with fields value representing numerical values. Here are sample documents from the numbers collection:

Input
Copied
Copy To Clipboard
[
  { "_id": ObjectId("609c26812e9274a86871bc6a"), "value": 10 },
  { "_id": ObjectId("609c26812e9274a86871bc6b"), "value": 15 },
  { "_id": ObjectId("609c26812e9274a86871bc6c"), "value": 20 }
]

🔄 Aggregation

Suppose we want to filter out documents where the value is divisible by 3. Here's how you can achieve this using the $mod operator:

example.js
Copied
Copy To Clipboard
db.numbers.aggregate([
  {
    $match: {
      value: { $mod: [3, 0] }
    }
  }
])

🧩 Explanation

  • $match: This stage filters documents based on the specified condition.
  • $mod: Performs the modulo operation, where the first value (3) is the divisor and the second value (0) is the expected remainder.

When discussing how the above aggregation works:

  • For the first document with value equal to 10, the remainder of 10 divided by 3 is not zero, so it doesn't match the condition and is filtered out.
  • For the second document with value equal to 15, the remainder of 15 divided by 3 is zero, so it matches the condition and is included in the output.
  • For the third document with value equal to 20, the remainder of 20 divided by 3 is not zero, so it doesn't match the condition and is filtered out.

💻 Output

Now, let's take a look at the output generated by the aggregation pipeline:

Output
Copied
Copy To Clipboard
{ "_id": ObjectId("609c26812e9274a86871bc6b"), "value": 15 }

📚 Use Cases

  1. Data Filtering:

    The $mod operator is useful for filtering documents based on divisibility criteria, allowing users to extract specific subsets of data.

  2. Data Transformation:

    Modulo operations can be employed to transform data or normalize values within a dataset.

  3. Periodic Computations:

    When dealing with time-series data or periodic phenomena, the $mod operator facilitates computations related to cycles or intervals.

🎉 Conclusion

The $mod operator in MongoDB's aggregation framework provides a powerful mechanism for performing modulo operations within aggregation pipelines. Whether you're filtering data, transforming values, or conducting periodic computations, mastering the usage of $mod empowers you to efficiently manipulate numerical data and extract meaningful insights from your datasets.

With its intuitive syntax and diverse applications, the $mod operator proves to be a valuable asset for handling numerical data effectively within MongoDB. Incorporate it into your aggregation pipelines to unlock new dimensions of data analysis 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