title: Express JS Middleware Mongoose filter and sort query i dont get what i expect to get and it's not sorting
body: <p><strong>this is the middleware to filter and sort</strong>
  I tried to modify this middleware but it's not work how I would like</p>
  <pre><code> const aliasTopMenu = (req, res, next) =&gt; {
    req.query.sort = '-price';
    req.query.fields = 'name,price,rating';
    req.query.limit = '3';
    next();
  };
  </code></pre>
  <p><strong>in router file</strong>
  just passe the middleware before function to get data
  express.Route().route('/top-5-menu').get(aliasTopMenu, getAllItems);</p>
  <p><strong>getAllItems function :</strong>
  this function to get all and filter some data as user want</p>
  <pre><code>const getAllItems = AsyncWrapper(async (req, res) =&gt; {
    //1)filtring
  
    const qeuryObj = { ...req.query };
    const excludField = ['page', 'sort', 'limit', 'fields'];
    excludField.forEach((el) =&gt; {
      delete qeuryObj[el];
    });
    //advanced filtring
    let queryStr = JSON.stringify(qeuryObj);
    queryStr = queryStr.replace(/(gt|gte|lte|lt)/g, (matched) =&gt; {
      return `$${matched}`;
    });
    let query = await Item.find(JSON.parse(queryStr));
  
    //2) sorting
  
    if (req.query.sort) {
      const sortBy = req.query.sort.split(',').join(' ');
      query = await Item.find(JSON.parse(queryStr)).sort(sortBy);
    }
    //3)Field limiting
    if (req.query.fields) {
      const fields = req.query.fields.split(',').join(' ');
  
      query = await Item.find(JSON.parse(queryStr)).select(fields);
    }
    //Pagination page and limit
    if (req.query.page) {
      const page = req.query.page * 1 || 1;
      const limit = req.query.limit * 1 || 100;
      const skip = (page - 1) * limit;
      const numItems = await Item.countDocuments();
      if (skip &gt;= numItems) {
        throw new Error('this page do not exist');
      }
      query = await Item.find(JSON.parse(queryStr)).skip(skip).limit(limit);
    }
  
    //execute query
  
    const items = await query;
  
    res.status(200).json(items);
  });
  </code></pre>
  <p><strong>resault to this query &quot;{{URL}}/items/top-5-menu&quot; {{URL}} is global variable containe localhost and prot</strong></p>
  <pre><code> [
      {
          &quot;rating&quot;: 4.5,
          &quot;_id&quot;: &quot;62aa6f0cb6cf327c8c4d219a&quot;,
          &quot;name&quot;: &quot;Pizza&quot;,
          &quot;price&quot;: 350
      },
      {
          &quot;rating&quot;: 4.5,
          &quot;_id&quot;: &quot;62aa6f15b6cf327c8c4d219c&quot;,
          &quot;name&quot;: &quot;Sawarma&quot;,
          &quot;price&quot;: 150
      },
      {
          &quot;rating&quot;: 4.5,
          &quot;_id&quot;: &quot;62acf53798c9c74218de901f&quot;,
          &quot;name&quot;: &quot;Burger&quot;,
          &quot;price&quot;: 250
      },
      {
          &quot;rating&quot;: 4.5,
          &quot;_id&quot;: &quot;62acf56298c9c74218de9021&quot;,
          &quot;name&quot;: &quot;Pizza&quot;,
          &quot;price&quot;: 400
      },
      {
          &quot;rating&quot;: 4.5,
          &quot;_id&quot;: &quot;62ad10569b344b2b223e6ea4&quot;,
          &quot;name&quot;: &quot;Shawarma&quot;,
          &quot;price&quot;: 500
      },
      {
          &quot;rating&quot;: 4.5,
          &quot;_id&quot;: &quot;62ad10709b344b2b223e6ea6&quot;,
          &quot;name&quot;: &quot;takos&quot;,
          &quot;price&quot;: 400
      },
      {
          &quot;rating&quot;: 4.5,
          &quot;_id&quot;: &quot;62ad10769b344b2b223e6ea8&quot;,
          &quot;name&quot;: &quot;takos&quot;,
          &quot;price&quot;: 250
      },
      {
          &quot;rating&quot;: 4.5,
          &quot;_id&quot;: &quot;62ad10839b344b2b223e6eaa&quot;,
          &quot;name&quot;: &quot;chiken&quot;,
          &quot;price&quot;: 400
      },
      {
          &quot;rating&quot;: 4.5,
          &quot;_id&quot;: &quot;62ad108f9b344b2b223e6eac&quot;,
          &quot;name&quot;: &quot;chiken&quot;,
          &quot;price&quot;: 350
      },
      {
          &quot;rating&quot;: 4.5,
          &quot;_id&quot;: &quot;62ad109b9b344b2b223e6eae&quot;,
          &quot;name&quot;: &quot;chiken&quot;,
          &quot;price&quot;: 350
      }
  ]
   
  </code></pre>
  <p><strong>dont sort data by price</strong></p>