The $mod operator performs modulo (remainder) math in MongoDB. It appears in two forms: as a query filter in find() and $match, and as an aggregation expression that computes remainders inside pipelines.
01
Remainder Math
dividend % divisor.
02
Two Forms
Query vs aggregation.
03
Divisibility
Filter multiples of N.
04
Odd / Even
Parity checks.
05
Use Cases
Buckets, sampling.
06
Argument Order
Context matters.
Fundamentals
Definition and Usage
Modulo returns the remainder after division. For example, 10 % 3 = 1, 15 % 3 = 0, and 20 % 3 = 2. MongoDB’s $mod lets you apply this logic to stored numeric fields.
In an aggregation expression, { $mod: [ dividend, divisor ] }computes and returns the remainder. In a query, { field: { $mod: [ divisor, remainder ] } }matches documents where field % divisor == remainder.
💡
Beginner Tip
The argument order differs between query and aggregation $mod. In queries, the second value is the expected remainder. In aggregation, the second value is the divisor you divide by.
Foundation
📝 Syntax
MongoDB uses $mod in two contexts. Learn both:
Aggregation Expression (returns remainder)
mongosh
{ $mod: [ <dividend>, <divisor> ] }
Query Operator (filters documents)
mongosh
{ <field>: { $mod: [ <divisor>, <remainder> ] } }
Syntax Rules
Aggregation — [dividend, divisor]; returns the computed remainder.
Query — [divisor, remainder]; matches when field % divisor == remainder.
Operands — can be field paths, literals, or numeric expressions.
Divisor zero — causes an error; always use a non-zero divisor.
Null input — aggregation $mod returns null if either operand is null.
💡 Query $mod vs Aggregation $mod
Query — { value: { $mod: [ 3, 0 ] } } → matches where value is divisible by 3
Inside $expr, aggregation $mod computes value % 2. When the result equals 0, the number is even. This pattern works for any parity or divisibility check in pipelines.
Example 4 — Assign Buckets for Sampling
Split records into 4 buckets using modulo for round-robin sampling:
10 and 20 become "even"; 15 becomes "odd". Combine $mod with $cond whenever you need categorical labels from numeric rules.
Applications
🚀 Use Cases
Divisibility filters — find records divisible by N (multiples of 5, 10, etc.).
Odd/even checks — parity logic for alternating workflows or A/B splits.
Data bucketing — assign IDs to buckets for sampling, sharding, or batch jobs.
Periodic patterns — group or filter cyclical numeric data (every 7th record, etc.).
🧠 How $mod Works
1
MongoDB reads the operands
Fields or literals are evaluated per document. Check whether you are in query or aggregation context.
Input
2
$mod applies modulo
Aggregation returns dividend % divisor. Query compares field % divisor to the expected remainder.
Compute
3
Result or match
Aggregation stores the remainder in a new field. Query includes or excludes documents based on the match.
Output
=
🔢
Remainder-based logic
Use for divisibility, parity, bucketing, and cyclic data patterns across queries and pipelines.
Wrap Up
Conclusion
The $mod operator brings modulo arithmetic to MongoDB queries and aggregation pipelines. Use the query form to filter by divisibility, and the aggregation form to compute remainders as new fields.
The most common beginner mistake is swapping the argument order. Remember: query uses [divisor, remainder], aggregation uses [dividend, divisor]. Next in the series: $month.
Check whether you need query $mod or aggregation $mod
Use query { $mod: [ N, 0 ] } for “divisible by N” filters
Combine aggregation $mod with $eq inside $expr
Guard against zero divisors in dynamic pipelines
Test with edge values: 0, negatives, and large numbers
❌ Don’t
Swap argument order between query and aggregation forms
Use aggregation syntax directly in a plain find() filter
Divide by zero — it throws an error
Assume $mod works on non-numeric strings
Confuse query $mod with JavaScript’s % operator syntax
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about $mod
Use these points when working with modulo logic in MongoDB.
5
Core concepts
🔢01
Remainder Math
dividend % divisor.
Purpose
📝02
Two Forms
Query vs aggregation.
Syntax
🛠03
Argument Order
Differs by context.
Critical
📊04
Divisibility
[N, 0] in queries.
Use case
⚠05
No Zero Divisor
Throws an error.
Edge case
❓ Frequently Asked Questions
$mod performs a modulo (remainder) operation. In aggregation expressions it returns dividend % divisor. In find() and $match queries it filters documents where a field's remainder matches a given value.
Aggregation expression: { $mod: [ <dividend>, <divisor> ] }. Example: { $mod: [ "$value", 3 ] } returns the remainder when value is divided by 3.
Query operator: { field: { $mod: [ <divisor>, <remainder> ] } }. Example: { value: { $mod: [ 3, 0 ] } } matches documents where value is divisible by 3.
Query $mod is a filter: [divisor, expected remainder]. Aggregation $mod is a math expression: [dividend, divisor] that computes and returns the remainder. Always check which context you are in.
In aggregation, if the divisor is zero, $mod throws an error. In queries, a zero divisor is invalid. Always ensure the divisor is non-zero.