title: How to sort documents in MongoDB by subtraction of length of inner arrays?
body: <p>I am retrieving data from my mongoDB database and I want to be able to sort documents by subtraction on length of inner arrays.
  My data structure looks like: </p>
  
  <pre><code>[
    {
     _id: "6bcdf5b7-0757-4195-a0b6-d7ec3d59d194",
     status: {
        plus: ['1', '2'],
        minus: ['1']
    },
    {
     _id: "6fb2ea76-8f12-4200-98fa-72201ba5f8c1",
     status: {
        plus: ['1', '2', '3', '4'],
        minus: []
    },
    {
     _id:"b1ecf353-acea-4808-a847-27b765c52230"
     status: {
        plus: ['1', '2', '3'],
        minus: ['1', '2', '3']
    },
  ...
  ]
  </code></pre>
  
  <p>What I am looking for - array of docs which is sorted by subtract of lengths <code>plus</code> and <code>minus</code> arrays.</p>
  
  <p>Result, that I am looking for: </p>
  
  <pre><code>[
     {
     _id: "6fb2ea76-8f12-4200-98fa-72201ba5f8c1",
     status: {
        plus: ['1', '2', '3', '4'],
        minus: [] // subtract - 4
    },
    {
     _id: "6bcdf5b7-0757-4195-a0b6-d7ec3d59d194",
     status: {
        plus: ['1', '2'],
        minus: ['1'] // subtrack - 1
    },
  
    {
     _id:"b1ecf353-acea-4808-a847-27b765c52230"
     status: {
        plus: ['1', '2', '3'],
        minus: ['1', '2', '3'] // subtrack - 0
    },
  ...
  ]
  </code></pre>
  
  <p>I found Aggregation framework, but I cant find a way to use it in my way.</p>
  
  <p>Of course, documents have much more properties in them, so group is not a best way, by my opinion.</p>
  
  <p>So my final result looks like this: </p>
  
  <pre><code>   col.aggregate([
          {
            $project: {
              status_count: {
                $subtract: [
                  { $size: { $ifNull: ['$status.plus', []] } },
                  { $size: { $ifNull: ['$status.minus', []] } }
                ]
              }
            }
          },
          {
            $sort: { status_count: -1 }
          }
        ])
  </code></pre>
  
  <p>Problems is: it looses all of other properties, including status object, I cant user additional find query, I cant skip and limit for the pagination.  </p>