简体   繁体   中英

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?

Eg 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"
   ]
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM