title: Mongoose - how to sort by upvote count, if it's a SUM?
body: <p>I have schemas <code>User</code> and <code>Thread</code>. They're related through a pivot schema called <code>Vote</code>: </p>
  
  <pre><code>const VoteSchema = new Schema({
    user: {
      type: ObjectId,
      ref: 'User',
      required: true,
    },
    thread: {
      type: ObjectId,
      ref: 'User',
      required: true,
    },
    value: {
      type: Number,
      required: true,
      validate: value =&gt; Promise.resolve(value === 1 || value === -1),
    },
  });
  </code></pre>
  
  <p>Needless to say, vote collection will have a reference to a thread, the user who voted, and the vote value which is 1 or -1.</p>
  
  <p>The Thread schema has also a reference to the vote:</p>
  
  <pre><code>...
  votes: [{
    type: ObjectId,
    ref: 'Vote',
  }],
  </code></pre>
  
  <p>So far I've read about <code>Thread.find().populate('votes')</code> which will make the votes property contains the documents instead of the ID, but I can't find how to do the SUM operation and order by that value.</p>
  
  <p>How can I sort <code>Thread.find()</code> to order based on the <code>SUM()</code> of it's 
  related <code>Vote</code>'s <code>value</code> property?</p>