$mod
The $mod query operator selects documents where a field value divided by a divisor has a specified remainder. This is useful for filtering documents based on modulo arithmetic conditions.
Parameters
-
divisor: The number to divide by. -
remainder: The expected remainder value.
Example (MongoDB Shell)
The following example demonstrates using the $mod operator to find all orders where the quantity is an odd number.
Create sample documents
db.orders.insertMany([ { _id: 1, item: "Widget", quantity: 15 }, { _id: 2, item: "Gadget", quantity: 20 }, { _id: 3, item: "Tool", quantity: 7 }, { _id: 4, item: "Device", quantity: 12 }, { _id: 5, item: "Part", quantity: 9 } ]);
Query example
db.orders.find({ quantity: { $mod: [2, 1] } });
Output
{ "_id" : 1, "item" : "Widget", "quantity" : 15 }
{ "_id" : 3, "item" : "Tool", "quantity" : 7 }
{ "_id" : 5, "item" : "Part", "quantity" : 9 }
This query returns documents where the quantity divided by 2 has a remainder of 1, effectively selecting all odd quantities.
Code examples
To view a code example for using the $mod query operator, choose the tab for the language that you want to use: