Operators: $mod

Checking whether the value of a field divided by a divisor has a specific remainder.

🪶 The $mod operator is used to perform a modulo operation on a field and compare the result with a specified value.


Database: Client > model: Users

Here we will simulate a database with the Users collection:
1
  • COLLECTION:
  • Users > [
    2  
  • {
  • name:
  • "Chris Axxwell",
  • nick:
  • "Chris",
  • age:
  • 27
  • }
  • ,
    3  
  • {
  • name:
  • "Chris Buckman",
  • nick:
  • "Chris",
  • age:
  • 28
  • ,
  • gender:
  • "Male"
  • }
  • ,
    4  
  • {
  • name:
  • "Santiago Smith",
  • nick:
  • "Lollipop",
  • age:
  • 24
  • ,
  • gender:
  • "Female"
  • }
  • ,
    5  
  • {
  • name:
  • "Justin Dacker",
  • nick:
  • "Justin",
  • age:
  • 22
  • }
  • ,
    6  
  • {
  • name:
  • "Chris Morgan",
  • nick:
  • "Mor",
  • age:
  • 25
  • }
  • ,
    7   ...
    8]

    Query the above database:

    1
  • import
  • MerlinDB,
  • {
  • Schema
  • }
  • from
  • "@chrisaxxwell/merlin-db";
    2
  • //Or if you are using 'merlindb.max.js' just call 'new MerlinDB()';
  • 3
    4
  • const
  • merlin =
  • new
  • MerlinDB(
  • )
  • ;
    5merlin
  • .connect(
  • "Client"
  • )
  • ;
    6
  • var
  • clientModel = merlin
  • .model(
  • "Users", <userSchema>
  • )
  • ;
    7
    8
  • //$mod: <[divisor, resto]>;
  • 9clientModel
  • .find(
  • {
  •   
    10
    11  
  • age:
  • {
  • $mod:
  • [
  • 2
  • ,
  • 1
  • ]
  • }
  • 12
    13
  • }
  • )
  • .then(
  • e =>
  • {
  • 14   console
  • .log(
  • e
  • )
  • ;
    15  
  • //returns:
  • 16   [  
    17      
  • {
  • name:
  • "Chris Axxwell",
  • nick:
  • "Chris",
  • age:
  • 27
  • }
  • ,
    18      
  • {
  • name:
  • "Chris Morgan",
  • nick:
  • "Mor",
  • age:
  • 25
  • }
  • 19      ...
    20   ]
    21
  • }
  • )