• Evaluation query operators
    • $mod
    • $regex

Evaluation query operators

Evaluation query operators return data based on the evaluation of a specified expression.
Operator| Description
---|---
$mod| Matches documents where the field element is divided by a given value and returns a the specified remainder value
$regex| Matches documents where a field matches a specified regular expression query
For the examples in this section, insert the following documents into the catalog collection:
1. db.catalog.insertMany([
2. {
3. product: 'bottle',
4. price: 15,
5. stock: 1
6. },
7. {
8. product: 'spoon',
9. price: 500,
10. stock: 0
11. },
12. {
13. product: 'cup',
14. price: 100,
15. stock: 14
16. },
17. {
18. product: 'BoWL',
19. price: 56,
20. stock: 5
21. },
22. {
23. product: 'boTtLe',
24. price: 20,
25. stock: 3
26. }
27. ])

$mod

Syntax : { <field>: { $mod: [ <divisor-value>, <modulus> ] } }
The $mod operator matches documents where the field element divided by a given value returns a specified remainder (otherwise known as a modulus). The mathematical operation for this is field-value % divisor-value = modulus.
Example: The following query returns all the documents where the value of the “stock” field is evenly divisible by 2:
1. db.catalog.find({
2. stock: {
3. $mod: [2, 0]
4. }
5. })

The output:
1. [
2. {
3. _id: ObjectId("63e3ac0184f488929a3f737a"),
4. product: 'spoon',
5. price: 500,
6. stock: 0
7. },
8. {
9. _id: ObjectId("63e3ac0184f488929a3f737b"),
10. product: 'cup',
11. price: 100,
12. stock: 14
13. }
14. ]

caution
Note that the $mod expression returns an error if you only have a single element in the array, more than two elements in the array, or if the array is empty. It also rounds down decimal input down to zero (e.g. $mod: [ 3.5 , 2 ] is executed as $mod: [ 3 , 2 ]).

$regex

Syntax : { <field>: { $regex: '<expression-string>', $options: '<flag>' } }
Other syntaxes: { <field>: { $regex: /<expression-string>/, $options: '<flag>' } } and { <field>: /<expression-string>/<flag> }.
To use regular expression for queries on particular fields, use the $regex operator.
Example: The following query returns all the documents where the value of the “product” field starts with the letter “b”:
1. db.catalog.find({
2. product: {
3. $regex: /^b/
4. }
5. })

The output:
1. [
2. {
3. _id: ObjectId("63e4ce469695494b86bf2b2d"),
4. product: 'bottle',
5. price: 15,
6. stock: 1
7. },
8. {
9. _id: ObjectId("63e4ce469695494b86bf2b31"),
10. product: 'boTtLe',
11. price: 20,
12. stock: 3
13. }
14. ]

$options is an optional parameter that specifies the regular expression flags to use, such as:
* Case-insensitivity (i)
* Multi-line matching (m)
* Dot character matching (s)

note
The regex flag for ignoring white spaces (x) is not currently supported. Follow here for more updates.
To perform case-insensitive matching, use the i flag in the regex expression.
Example: The following query returns all the documents where the value of the “product” field is equal to “bottle” (case-insensitive):
1. db.catalog.find({
2. product: {
3. $regex: /bottle/i
4. }
5. })

The output:
1. [
2. {
3. _id: ObjectId("63e3ac0184f488929a3f7379"),
4. product: 'bottle',
5. price: 15,
6. stock: 1
7. },
8. {
9. _id: ObjectId("63e3ac0184f488929a3f737d"),
10. product: 'boTtLe',
11. price: 20,
12. stock: 3
13. }
14. ]