title: How do I build a dynamic query in Mongoose?
body: <p>I need to build a query using the following sample data:</p>
  
  <pre><code>{
     brandFilters:[ { brand: 'Amana', checked: true },
       { brand: 'Whirlpool', checked: true } ],
     priceFilters:[ { low: 100, high: 200, label: '$100 - $200', checked: true },
       { low: 200, high: 300, label: '$200 - $300', checked: true } ],
    rating: null }
  </code></pre>
  
  <p>The first segment of the query builds the brands criteria as shown here;</p>
  
  <pre><code>let brands = [];
  let productQuery = Product.find();
  
  if(filters.brandFilters.length) {
      for(let filter of filters.brandFilters) {
          brands.push(filter.brand)
      }
  
      productQuery.where('brand').in(brands)
      console.log('brands', brands)    
  }
  </code></pre>
  
  <p>The second segment is supposed build the price criteria as shown here:</p>
  
  <pre><code>if(filters.priceFilters.length) {
      for(let filter of filters.priceFilters) {
          productQuery.where('price').gte(+filter.low).lte(+filter.high);
      }
  }     
  </code></pre>
  
  <p>The intention is to add a "where" clause for each price filter as shown in the data above. The problem is multiple where clauses for the price criteria are not being added. Each successive price filter in the for loop overwrites the last. So instead of having multiple "where" clauses for my price filters, the query only contains one for the last iteration of the for loop.</p>
  
  <p>I am trying to get to a query that would look like this:</p>
  
  <pre><code>productQuery.where('brand').in(['Amana', 'Whirlpool'])
  productQuery.where('price').gte(100).lte(200)
  productQuery.where('price').gte(200).lte(300)
  </code></pre>