【问题标题】:Node Sequelize find where $like wildcardNode Sequelize 查找 $like 通配符的位置
【发布时间】:2021-07-29 22:48:30
【问题描述】:

我正在尝试向 Node Sequelize findAll 添加 where like 子句,以使用以下代码表现得像 sql 查询 select * from myData where name like '%Bob%'

let data: Array<any> = await MyDataSequelizeAccess.findAll({
  where: {
    name: {
      $like: `%Bob%`
    }
  }
});

返回以下错误

无效值 { '$like': '%Bob%' }

如何在我的 sequelize 对象上执行那种类型的 where 通配符或 where like?

let data: Array<any> = await MyDataSequelizeAccess.findAll({
  where: {
    name: `Bob`
  }
});

这按预期工作,但我无法让通配符工作。

更新仍然没有骰子 - 根据 https://sequelize.readthedocs.io/en/latest/docs/querying/#operators 我的语法看起来正确

我也在尝试(但失败)对 $not 做同样的事情

let data: Array<any> = await MyDataSequelizeAccess.findAll({
  where: {
    name: {
      $not: `Bob`
    }
  }
});

并得到与上述相同的错误Invalid value { '$not': '%Bob%' }

【问题讨论】:

    标签: sequelize.js


    【解决方案1】:

    字符串操作符(例如$operator)是 Sequelize V5 中的遗留[1]。 您仍然可以使用传统方式使用运算符执行查询,但必须使用运算符的别名设置连接(对此会有弃用警告)。 [2]

    const Op = Sequelize.Op;
    const operatorsAliases = {
      $like: Op.like,
      $not: Op.not
    }
    const connection = new Sequelize(db, user, pass, { operatorsAliases })
    
    [Op.like]:  '%Bob%' // LIKE '%Bob%'
    $like: '%Bob%' // same as using Op.like (LIKE '%Bob%')
    

    Sequelize.js 的更新文档将Sequelize.Op 模块中的运算符作为符号运算符提供。 [3]

    Sequelize 默认使用 Symbol 操作符,将操作符注入查询的风险降到最低;建议继续使用符号运算符。 [4]

    const Sequelize = require('sequelize');
    const Op = Sequelize.Op;
    
    let data: Array<any> = await MyDataSequelizeAccess.findAll({
      where: {
        name: {
          [Op.like]: '%Bob%'
        }
      }
    });
    

    【讨论】:

    • 如何在字段名称中使用较低的功能进行相同的查询?
    • 使用变量代替 '%Bob%' 名称:{ [Op.like]: %${name}% }
    【解决方案2】:

    快速续集 首先像这样导入 express 和 defile Op:

    const Sequelize = require("sequelize");
    const Op = Sequelize.Op;
    
    const { search } = await req.body;
    

    这样做:

    const users = await User.findAll({
        where: {
          username: { [Op.like]: `%${search}%` },
        },
        include: [{ model: Tweet, as: "Tweets" }],
        raw: true,
      }).catch(errorHandler);
    

    【讨论】:

      【解决方案3】:

      如果有人正在寻找与 express.js 一起使用的示例

      GET 请求正文

       {
          "searchString": "search",
          "count": 0,
          "limit": 20
      }
      

      express.js 与续集

      app.get("/api/saloon", function (req, res) {
      Saloon.findAll({
          where: {
              name: {[Op.iLike]: `%${req.body.searchString}%`}
          },
          offset: req.body.count,
          limit: req.body.limit,
      })
      

      【讨论】:

        【解决方案4】:

        我们可以使用运算符 [Op.substring] 而不是 [Op.like],因此我们不必附加“%”符号。

        await Model.findAll({
            where: {
                name: { [Op.substring]: "bob" }
            }
        });
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-09-06
          • 2021-04-04
          • 2021-06-23
          • 2015-08-05
          • 2017-06-28
          • 1970-01-01
          相关资源
          最近更新 更多