title: Building a product-filter in NodeJS/MongoDB: only works when selecting all filters
body: <p>I am building a product-filter in nodeJS. It works when i <strong>hard-code</strong> the following find()</p>
  <pre><code> const products = await Product.find({
              price: {
                $gte: price[0],
                $lte: price[1],
              }, category: categories, shipping: shipping})
  </code></pre>
  <p>The problem is of course that it only gives result if entered all the search options in the front-end.
  However i also want the filter to work if i choose to <strong>NOT</strong> enter all the search options.
  Thus i came up with the following code, to store all filters in an array, but it doesn't give any results.
  <strong>The output of the array is =</strong></p>
  <pre><code>  { price: { '$gte': 0, '$lte': 40820 } },
    {
      category: [
        '60657e1d328b25043581e46f',
        '601ef1ea8e06fc04cdaa5080',
        '601eeaeb8e06fc04cdaa507f'
      ]
    },
    { shipping: 'Yes' }
  ] &lt;&lt;&lt;_--- content of searchArry????
  </code></pre>
  <p><strong>This is my code, but it returns no results</strong> =</p>
  <pre><code>exports.searchFilters = async (req, res) =&gt; {
    try {
    const categories = req.body.categoryIds;
    const price = req.body.price; 
    const shipping = req.body.shipping;
    console.log(price, &quot;&lt;&lt;&lt;------- price data received in backend???&quot;); 
    console.log(categories, &quot;&lt;&lt;--- categories data received in backend???&quot;); 
    console.log(shipping, &quot;&lt;&lt;---- shipping data received in backend&quot;); 
    
    const searchArray = [];
   
    if (req.body.price) {
      const price = {
        $gte: req.body.price[0],
        $lte: req.body.price[1],
      }
      searchArray.push({ &quot;price&quot; : price });
    }
  
    if (req.body.categoryIds) {
      
  
      searchArray.push({ &quot;category&quot; : categories });
    }
  
    if (req.body.shipping) {
      
      searchArray.push({ &quot;shipping&quot; : shipping });
    }
  
          console.log(searchArray, &quot;&lt;&lt;&lt;_--- content of searchArry????&quot;);
  
    const products = await Product.find({searchArray})
    .populate(&quot;category&quot;, &quot;_id name&quot;) 
    .populate(&quot;subs&quot;, &quot;_id name&quot;) 
    .populate(&quot;postedBy&quot;, &quot;_id name&quot;) 
    .exec(); 
    
    res.json(products);
    // console.log(products, &quot;&lt;&lt;--producte?? &quot;) console.log(products.length, &quot;&lt;&lt;--- lengte&quot;) } catch(err) { console.log(err); }}
    } catch(err) {
      console.log(err);
    }
  }
  </code></pre>