SReca
SReca

Reputation: 673

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.

Upvotes: 8

Views: 12497

Answers (1)

pierreaurelemartin
pierreaurelemartin

Reputation: 1682

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 ?

Upvotes: 17

Related Questions