Mongoose-Paginate - Sorting by a populated field
<p>I am using mongoose-paginate to get a list of records sorted by a field which was populated from a reference.</p>

<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
<div class="snippet-code">
<pre class="snippet-code-js lang-js prettyprint-override"><code>// models

const Company = new Schema({
  name: String,
  type: String
});

const report = new Schema({
  _company: { 
    type: String, 
    ref: 'Company' 
  },
  name: String
});

let options = {
  offset: 0,
  limit:10,
  populate: '_company',
  select: 'type _company',
  sort: { '_company.name': 1 },
  lean: true
};

// paginate.
report.paginate({},options, (err, results) =&gt;  {
  if(err) {
    console.log(err);
  }
  
  res.jsonp({
    data: results
  });
})</code></pre>
</div>
</div>
</p>

<p>Is there a way to sort by the field _company.name, which is a populated field.</p>

<p>added sample code below.</p>

<p>Thanks.</p>