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:
{ $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:
[
{ "_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:
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:
{ "_id": ObjectId("609c26812e9274a86871bc6b"), "value": 15 }
📚 Use Cases
Data Filtering:
The
$mod
operator is useful for filtering documents based on divisibility criteria, allowing users to extract specific subsets of data.Data Transformation:
Modulo operations can be employed to transform data or normalize values within a dataset.
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:
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 $mod Operator), please comment here. I will help you immediately.