1. 简介
Sequelize
是一个基于 Node.js 的 ORM 框架,支持多种数据库。Op
(Operators)是其提供的查询操作符,用于构建复杂的过滤条件。通过 Op
可以实现 WHERE
子句中的逻辑比较、模糊查询、范围查询等操作。
提示 :使用
Op
前需从 Sequelize 实例引入
javascript
const { Op } = require('sequelize');
2. 常用 Op
操作符及示例
2.1 基础比较操作符
操作符 | 描述 | 示例代码 |
---|---|---|
Op.eq |
等于 | { age: { [Op.eq]: 25 } } → WHERE age = 25 |
Op.ne |
不等于 | { status: { [Op.ne]: 'pending' } } → WHERE status != 'pending' |
Op.gt |
大于 | { age: { [Op.gt]: 18 } } → WHERE age > 18 |
Op.gte |
大于等于 | { price: { [Op.gte]: 100 } } → WHERE price >= 100 |
Op.lt |
小于 | { age: { [Op.lt]: 30 } } → WHERE age < 30 |
Op.lte |
小于等于 | { stock: { [Op.lte]: 0 } } → WHERE stock <= 0 |
示例代码:
javascript
const users = await User.findAll({
where: {
age: {
[Op.gt]: 18,
[Op.lt]: 30
}
}
});
// 查询年龄在 18~30 之间的用户
2.2 范围查询
操作符 | 描述 | 示例代码 |
---|---|---|
Op.between |
在范围内 | { age: { [Op.between]: [20, 30] } } → WHERE age BETWEEN 20 AND 30 |
Op.notBetween |
不在范围内 | { price: { [Op.notBetween]: [50, 100] } } |
Op.in |
在数组中 | { id: { [Op.in]: [1, 2, 3] } } → WHERE id IN (1, 2, 3) |
Op.notIn |
不在数组中 | { role: { [Op.notIn]: ['admin', 'root'] } } |
示例代码:
javascript
// 查询状态为 'active' 或 'verified' 的用户
const users = await User.findAll({
where: {
status: {
[Op.in]: ['active', 'verified']
}
}
});
2.3 模糊查询
操作符 | 描述 | 示例代码 |
---|---|---|
Op.like |
区分大小写的模糊匹配 | { name: { [Op.like]: '%john%' } } → WHERE name LIKE '%john%' |
Op.notLike |
排除模糊匹配 | { title: { [Op.notLike]: '%test%' } } |
Op.iLike |
不区分大小写的模糊匹配 (PostgreSQL) | { email: { [Op.iLike]: '%EXAMPLE.COM' } } |
Op.startsWith |
以字符串开头 | { name: { [Op.startsWith]: 'Alex' } } → WHERE name LIKE 'Alex%' |
Op.endsWith |
以字符串结尾 | { path: { [Op.endsWith]: '.jpg' } } → WHERE path LIKE '%.jpg' |
Op.substring |
包含字符串(同 Op.like + % %) |
{ description: { [Op.substring]: 'urgent' } } |
示例代码:
javascript
// 查询名字以 'A' 开头、包含 'dmin' 的用户(不区分大小写)
const admins = await User.findAll({
where: {
[Op.and]: [
{ name: { [Op.startsWith]: 'A' } },
{ role: { [Op.iLike]: '%dmin%' } }
]
}
});
2.4 逻辑操作符
操作符 | 描述 | 示例代码 |
---|---|---|
Op.and |
逻辑与 | { [Op.and]: [{ a: 5 }, { b: 6 }] } → WHERE (a = 5 AND b = 6) |
Op.or |
逻辑或 | { [Op.or]: [{ age: 18 }, { isAdult: true }] } |
Op.not |
逻辑非 | { [Op.not]: { role: 'guest' } } → WHERE NOT (role = 'guest') |
示例代码:
javascript
// 查询年龄大于 30 或状态为 'inactive' 的用户
const filteredUsers = await User.findAll({
where: {
[Op.or]: [
{ age: { [Op.gt]: 30 } },
{ status: 'inactive' }
]
}
});
2.5 其他操作符
操作符 | 描述 | 示例代码 |
---|---|---|
Op.is |
是否为 NULL |
{ deletedAt: { [Op.is]: null } } → WHERE deletedAt IS NULL |
Op.notNull |
不为 NULL |
{ email: { [Op.notNull]: true } } → WHERE email IS NOT NULL |
Op.any |
匹配数组中的任意值 (PostgreSQL) | { id: { [Op.any]: [1, 2, 3] } } → WHERE id = ANY (ARRAY[1,2,3]) |
Op.col |
跨列比较 | { where: { amount: { [Op.gt]: Sequelize.col('price') } } } |
示例代码:
javascript
// 查询未删除的用户
const activeUsers = await User.findAll({
where: {
deletedAt: { [Op.is]: null }
}
});
3. 综合示例
javascript
const { Op } = require('sequelize');
// 查询年龄在 20-30 之间、状态为 active 或 verified,且邮箱不为空的用户
const results = await User.findAll({
where: {
[Op.and]: [
{ age: { [Op.between]: [20, 30] } },
{
[Op.or]: [
{ status: 'active' },
{ status: 'verified' }
]
},
{ email: { [Op.notNull]: true } }
]
}
});