title: How can I merge two queries in mongodb?
body: <p>To sort the documents inside Ads Collection I am using the below query which takes parameters from the URL and its working perfectly.</p>
  <pre><code>router.get(&quot;/&quot;, auth, async (req, res) =&gt; {
    let query;
  
    let queryStr = JSON.stringify(req.query);
  
    queryStr = queryStr.replace(
      /\b(gt|gte|lt|lte|in)\b/g,
      (match) =&gt; `$${match}`
    );
  
    console.log(queryStr);
  
    query = Ads.find(JSON.parse(queryStr));
    const ads = await query;
  
  
    res.status(200).json({ data: ads });
  });
  </code></pre>
  <p>I am using the text operator in the Ads Collection for searching with the below route .</p>
  <pre><code>router.get(&quot;/find/:query&quot;, (req, res) =&gt; {
    let query = req.params.query;
    
    Ads.find(
      {
        $text: { $search: query },
      },
      function (err, result) {
        if (err) throw err;
        if (result) {
          res.json(result);
        } else {
          res.send(
            JSON.stringify({
              error: &quot;Error&quot;,
            })
          );
        }
      }
    );
  });
  </code></pre>
  <p>Both the routes are working perfectly but <strong>How can I merge the above two in one?</strong></p>
  <p>For e.g, I want to do a text search on the first route after getting a response, and similarly for the second route, after getting a response I want to apply the query parameters and get a response .</p>
  <p><strong>How can I merge the above two to get the desired output?</strong></p>