Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
The $mod
query operator is used to filter documents based on the remainder of a division operation. This operator allows for queries that involve mathematical conditions, such as finding values divisible by a number or having a specific remainder. It's supported in Azure Cosmos DB for MongoDB (vCore).
{
<field>: { $mod: [ <divisor>, <remainder> ] }
}
<field>
: The field on which to perform the modulus operation.<divisor>
: The number by which to divide the field's value.<remainder>
: The expected remainder after the division.
db.collection.find({ "sales": { "$mod": [3, 0] } })
This example retrieves all documents where the sales
field value is divisible by 3 (that is, the remainder is 0). It will produce the following output:
[
{ "_id": 1, "sales": 9, "product": "A" },
{ "_id": 2, "sales": 15, "product": "B" },
{ "_id": 3, "sales": 21, "product": "C" }
]
db.collection.find({ "totalSales": { "$mod": [5, 2] } })
This query filters documents where the totalSales
field has a remainder of 2 when divided by 5. It will produce the following output:
[
{ "_id": 4, "totalSales": 7, "product": "X" },
{ "_id": 5, "totalSales": 12, "product": "Y" },
{ "_id": 6, "totalSales": 17, "product": "Z" }
]
db.collection.find({ "sales.monthly.total": { "$mod": [4, 1] } })
This example demonstrates querying a nested field (sales.monthly.total
) with the $mod
operator. It will produce the following output:
[
{ "_id": 7, "sales": { "monthly": { "total": 5 } }, "category": "Electronics" },
{ "_id": 8, "sales": { "monthly": { "total": 9 } }, "category": "Clothing" }
]
- The
$mod
operator is applied to numerical fields only. Using it on non-numerical fields result in an error. - Ensure that the divisor isn't zero, as it leads to an invalid operation.
- Review options for Migrating from MongoDB to Azure Cosmos DB for MongoDB (vCore)
- Read more about Feature compatibility with MongoDB