如何在 SequelizeJS 中使用 $in 运算符?

2023-12-19

我尝试在 SequelizeJS 中使用 $in 运算符,就像在 MySQL 语句中一样:

SELECT column_name(s)
FROM table_name
WHERE column_name IN (value1, value2, ...);

就我而言,我加入了三个表,但我认为这与该问题无关。我不断收到此错误Error: Invalid value { '$in': ['12345','67890','15948'] }使用以下代码:

definedTable.findAll({
  include: [{
    model: definedTableTwo,
    where: {
      zip_code: {
        $in: ['12345','67890','15948']
      }
    },
    required: true,
    plain: true
  },
  {
    model: definedTableThree,
    required: true,
    plain: true
  }]
})

有人可以提供有关如何使用 $in 运算符的一些见解吗?我见过的唯一例子是在搜索 Id 时使用数组中的整数。


只需阅读此处的文档续集 WHERE http://docs.sequelizejs.com/manual/tutorial/querying.html#where

我会尝试:

const Op = Sequelize.Op;
definedTable.findAll({
  include: [{
    model: definedTableTwo,
    where: {
      zip_code: {
        [Op.in]: ['12345','67890','15948']
          }
    },
    required: true,
    plain: true
  },
  {
    model: definedTableThree,
    required: true,
    plain: true
  }]
})

这有帮助吗?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 SequelizeJS 中使用 $in 运算符? 的相关文章