Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

111
Views
Nesting multiple [Op.not] queries in a single where clause

I've got a filter that allows users to indicate themes and genres they would like removed from the search results. This is an OR, not AND situation, so if either a theme or a genre is matched, it should be removed from the pool.

If I run the query below with a single [Op.not] clause, the themes and genres filter correctly. When I attempt to combine them as show in the example, only the genres [Op.not] executes. I assume I've chained them incorrectly in the where clause?

Code Example

    //Variables to store filters from JSON body
    let sortBy = [req.body.galleryFilters.sortBy, 'ASC']
    let hiddenThemes = [];
    let hiddenGenres = [];

    //Parse the body for activeFilters create and array of Active Filters 
    req.body.galleryFilters.themes.forEach(theme => {
      if(theme.hidden === true) {
        hiddenThemes.push(theme.name)
      }
    })

    req.body.galleryFilters.genres.forEach(genre => {
      if(genre.hidden === true) {
        hiddenGenres.push(genre.name)
      }
    })

    //use variables above to create new playlist obje
    const playlists = await db.playlists.findAll({
      where: {
        [Op.not]: {
          themes: {
            [Op.overlap]: hiddenThemes
          },
        },
        [Op.not]: {
          genres: {
            [Op.overlap]: hiddenGenres
          },
        },
      },
      order: [[req.body.galleryFilters.sortBy, 'ASC']]
    });
  
    return res.send(playlists);
  
  }
});
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I'm too inexperienced to give a thorough explanation but the revision below seems to work for now:

 const playlists = await db.playlists.findAll({
      where: {
        [Op.not]: {
          [Op.or]: {
            themes: { [Op.overlap]: hiddenThemes },
            genres: { [Op.overlap]: hiddenGenres },
          }
        }
      },
      order: [[req.body.galleryFilters.sortBy, 'ASC']]
    });

    return res.send(playlists);

  }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!