1

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"
   ]
}
2
  • check out this thread stackoverflow.com/a/48369512/12761193 Commented Sep 19, 2020 at 5:35
  • @Arya thanks for the solution. you are my lifesaver :-) Commented Sep 20, 2020 at 12:34

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.