Amol Ghatol
Amol Ghatol

Reputation: 319

How to postgresql sequelize query on json array

How to write the sequelize where query on medium and subject over the PostgreSQL table in NodeJs?

E.g. select * from tablename where params.medium IN("Hindi", "English") AND params.subject IN("Hindi","Science");

here table column name is params

Data in params column:

{
   "board":"CBSE",
   "medium":[
      "Hindi",
      "English",
      "Urdu"
   ],
   "subject":[
      "Hindi",
      "Mathematics",
      "Science"
   ]
}

Upvotes: 1

Views: 880

Answers (1)

Amol Ghatol
Amol Ghatol

Reputation: 319

I have done as following

   const Op = Sequelize.Op;


    const filter = {
      [Op.and]: [{
        [Op.or]:[
          {
            'params.medium': {
              [Op.regexp]: 'Hindi'
            }
          },
          {
            'params.medium': {
              [Op.regexp]: 'English'
            }
          }
        ],
      },
      {
        [Op.or]:[
          {
            'params.subject': {
              [Op.regexp]: 'Hindi'
            }
          },
          {
            'params.subject': {
              [Op.regexp]: 'Science'
            }
          }
        ],
      }
    ]};

    model.findAll({
        where: {
          ...filter
        }
   )};

Please suggest if any better solution.

Upvotes: 1

Related Questions