📅  最后修改于: 2023-12-03 15:34:56.481000             🧑  作者: Mango
Sequelize is an Object Relational Mapping (ORM) library for Node.js which provides an easy-to-use interface for interacting with a database using JavaScript. One of the most commonly used operators in Sequelize is not equal
which allows us to filter data in a database for all rows where a specific column does not match a certain value.
In Sequelize, the not equal
operator is denoted by ne
. We can use this operator to filter data in a database based on a column value.
const { Op } = require('sequelize');
Model.findAll({
where: {
column_name: {
[Op.ne]: 'value'
}
}
})
In the above example, column_name
is the name of the column we want to filter, and value
is the value we want to exclude. The [Op.ne]
syntax is used to denote the not equal operator.
Let's consider an example where we have a table named users
in our database which stores information about all the registered users. In order to get all the users whose age is not equal to 25, we can use the not equal
operator as follows:
const { Op } = require('sequelize');
const User = require('../models/user');
User.findAll({
where: {
age: {
[Op.ne]: 25
}
}
})
In the above example, User
is the model we defined to interact with the users
table. We are using the findAll
method to get all the users whose age is not equal to 25.
In this article, we discussed how to use the not equal
operator in Sequelize to filter data in a database. We saw how to define the not equal
operator using the [Op.ne]
syntax and how to use it to filter rows based on a column value.