【问题标题】:How to use the $in operator with SequelizeJS?如何在 SequelizeJS 中使用 $in 运算符?
【发布时间】:2018-09-16 22:39:45
【问题描述】:

我正在尝试在 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 时使用数组中的整数。

【问题讨论】:

    标签: javascript mysql node.js sequelize.js in-operator


    【解决方案1】:

    只是在这里阅读文档Sequelize 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
      }]
    })
    

    这有帮助吗?

    【讨论】:

    • 嗨@pierreaurelemartin,$in 本质上是[Op.in]$in 是该运算符的别名。我质疑我是否以正确的格式提供值。至少这是错误所表明的。
    • 我刚刚意识到别名$in 包含在我的文件中,这最终成为了问题。基本上是用户错误。我会将您的评论标记为答案,因为它在技术上是正确的,并且暴露了我的用户错误。谢谢
    • 大家好,我也有同样的问题。相反,我需要在邮政编码上添加和条件。你有什么想法吗?
    猜你喜欢
    • 2022-01-01
    • 1970-01-01
    • 2019-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多