Filters

Filter system, use to filter results

PropertyDescriptionType usage

$gt

is greater than

$gte

is greater than or equal to

$lt

is less than

$lte

is less than or equal to

$eq

is equal to

$ne

is not equal to

$in

is included in

$nin

is not included in

$exists

is defined

Any

$regex

is a regular expression

$limit

Limit how much data will come out in the result

To use, just create an object, its key will be the value that we will apply the filters

<key>: { $filter: value }

Teacher.find({
  age: {
    $gte: 20
  },
  score: {
    $gte: 5
  }
})

Output

[
  {
    _id: '628c075c147b594cc84b',
    id_students: [ 123, 1234 ],
    score: 10,
    age: 20
  },
  {
    _id: '628c075c147b594cc8ac0a4b',
    id_students: [ 1234, 123 ],
    score: 5,
    age: 20
  }
]

Reference with filter

The filter can be applied inside finds with references

Teacher.find({
  age: 20,
  tb_students: {
    $ref: "Students",
    $id: "$data.id_students",
    age: { $gte: 6, $lte: 10 }
  }
})

Full example

Teacher.find({
  $limit: 2,
  score: {
    $gte: 5
  },
  tb_students: {
    $ref: "Students",
    $id: "$data.id_students",
    age: { $gte: 6, $lte: 10 }
  }
});

Last updated