title: How to chain queries to mongoDB database (mongoose) based on condition
body: <p><strong>Source Code</strong></p>
  <pre><code>let placeForSearch=&quot;hampi&quot;
  let filteredHotelFacilities=req.body.filter //[&quot;Free Wifi&quot;]
  
  let hotels = await Hotel.find({placeForSearch})
        .where(&quot;facilities&quot;)
        .all(filteredHotelFacilities)
        .in(filteredHotelFacilities)
        .select(selectedProperties)
        .skip(pageNumber * pageSize)  
        .limit(pageSize);
  </code></pre>
  <p>The above code finds if any elements of <code>req.body.filter</code> is in the facilities array in the database and the above code outputs the collection given below.</p>
  <pre><code>  {
      facilities: [ 'Garden', 'Free Wifi' ],
      _id: 60f066e61e6971412cf5e727,
      placeForSearch: 'hampi'
    },
    {
      facilities: [ 'Free Wifi' ],
      _id: 60f180141121791e2884d6f0,
      placeForSearch: 'hampi'
    }
  </code></pre>
  <p><strong>Database</strong></p>
  <pre><code>  {
      facilities: [ 'Garden', 'Free Wifi' ],
      _id: 60f066e61e6971412cf5e727,
      placeForSearch: 'hampi'
    },
    {
      facilities: [ 'Garden', 'AC' ],
      _id: 60f066e61e6971412cf5e727,
      placeForSearch: 'hampi'
    },
    {
      facilities: [ 'Free Wifi' ],
      _id: 60f180141121791e2884d6f0,
      placeForSearch: 'hampi'
    }
  </code></pre>
  <p>Facilities array in the database can have any of the below-given elements or all of them.</p>
  <pre><code>  [
      &quot;Free Wifi&quot;,
      &quot;Garden&quot;,
      &quot;Water park&quot;,
      &quot;Spa and wellness centre&quot;,
      &quot;Terrace&quot;,
      &quot;Fitness centre&quot;,
      &quot;Restaurant&quot;,
      &quot;Room service&quot;,
      &quot;Bar&quot;,
      &quot;Hot tub/jacuzzi&quot;,
      &quot;Swimming pool&quot;,
      &quot;AC&quot;
    ];
  </code></pre>
  <p><strong>So how can I get all collections in the database if the user does not apply any filter?(<code>req.body.filter=[]</code>)</strong></p>
  <p><strong>In other words how can i remove <code>.all(filteredHotelFacilities).in(filteredHotelFacilities)</code> based on condition.</strong></p>