Filters

Filter system, use to filter results

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