Search code examples
javascriptmysqlnode.jssequelize.jsin-operator

How to use the $in operator with SequelizeJS?


I'm trying to use the $in operator in SequelizeJS as you would in a MySQL statement:

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

In my case, I've joined three tables, but I don't believe that's related to the issue. I keep getting this error Error: Invalid value { '$in': ['12345','67890','15948'] } with the following code:

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

Can someone provide some insight on how to use the $in operator? The only examples I've seen are with integers within the array when searching Ids.


Solution

  • Just reading the docs here Sequelize WHERE

    I'd try :

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

    Does that help ?