title: Get difference of values between two dates
body: <p>I have this documents</p>
  
  <pre><code>{
      "url" : "/url1.php",
      "date" : ISODate("2016-01-02T00:00:00.000Z"),
      "hits" : 104
  },
  {
      "url" : "/url2.php",
      "date" : ISODate("2016-01-02T00:00:00.000Z"),
      "hits" : 121
  },
  {
      "url" : "/url3.php",
      "date" : ISODate("2016-01-02T00:00:00.000Z"),
      "hits" : 75
  },
  {
      "url" : "/url1.php",
      "date" : ISODate("2016-05-11T00:00:00.000Z"),
      "hits" : 100
  },
  {
      "url" : "/url2.php",
      "date" : ISODate("2016-05-11T00:00:00.000Z"),
      "hits" : 104
  }
  </code></pre>
  
  <p>And I want to get difference between two dates, sort by this difference. If some document not exist in second date then the difference is first value.</p>
  
  <pre><code>{
      "url" : "/url1.php",
      "diff": 4
  },
  {
      "url" : "/url2.php",
      "diff": 17
  },
  {
      "url" : "/url3.php",
      "diff": 75
  }
  </code></pre>
  
  <p>The url3.php not exists in second date.</p>
  
  <p>I have tried this </p>
  
  <pre><code>db.collection.aggregate(
  {
     $match : 
        {
         $or:[{"date":{$gte:ISODate("2016-01-02"),$lte:ISODate("2016-01-02")}},{"date":{$gte:ISODate("2016-05-11"),$lte:ISODate("2016-05-11")}}],
         }
     },
     {$group:{_id:{"url":"$url",month:{$month:"$date"}},total:{"$sum":"$visitas"}}},
     {$group:{_id:{"url":"$_id.url",super:{"$subtract":[{$max:"$total"},{$min:"$total"}]}},max:{$first:"$total"},min:{$last:"$total"}}},
      {$group:{_id:{"url":"$_id.url",difference:{"$subtract":["$max","$min"]},"dateHight":"$max","dateLow":"$min","diff2":"$super"}}},
      {$unwind:"$_id.difference"},
      {$match:{"_id.difference":{$ne:0}}},
     {$sort:{"_id.difference":-1}}
  )
  </code></pre>
  
  <p>This show</p>
  
  <pre><code>{
      "url" : "/url1.php",
      "diff": 4
  },
  {
      "url" : "/url2.php",
      "diff": 17
  }
  </code></pre>
  
  <p>But not url3.php because not exists in second date</p>