Sequelize 中使用 Op 的过滤查询汇总指南!最全面!

517 阅读2分钟

1. 简介

Sequelize 是一个基于 Node.js 的 ORM 框架,支持多种数据库。Op(Operators)是其提供的查询操作符,用于构建复杂的过滤条件。通过 Op 可以实现 WHERE 子句中的逻辑比较、模糊查询、范围查询等操作。

提示:使用 Op 前需从 Sequelize 实例引入

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

示例代码:

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'] } }

示例代码:

// 查询状态为 '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' } }

示例代码:

// 查询名字以 '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')

示例代码:

// 查询年龄大于 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') } } }

示例代码:

// 查询未删除的用户
const activeUsers = await User.findAll({
  where: {
    deletedAt: { [Op.is]: null }
  }
});

3. 综合示例

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 } }
    ]
  }
});