'Sequelize select * where attribute is NOT x

looking at the docs you can use model.findAll({where: {attribute: x}}). However, I want to select all attributes that are simply NOT x. I was looking into a regular expression here but that seemed like not an optimal solution.

What is the best way to do this?



Solution 1:[1]

Try this

model.findAll({where: {attribute: { $not: 'x'}}})

Sequelize actually provides many operators for you to get your filter data.

You can refer to the link here

Solution 2:[2]

Updated method, for modern Sequelize:

model.findAll({
  where: {
    someAttribute: {
      [sequelize.Op.not]: 'some value'
    }
  }
});

Solution 3:[3]

Please note the $not:'x is deprecated and does not work in the latest version.

Now we can use [Op.gt instead:

model.findAll({
  where: {
    attribute: {[Op.not]:'x'}
  }
});

Please find more details here

Solution 4:[4]

model.findAll({
  where: {
    attribute: {[Op.ne]:'x'}
  }
});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Eunice Poh
Solution 2 Brad
Solution 3 Wadson Vaval
Solution 4 Eng xtian