title: MongoDb aggregation query using $project and $sort
body: <p>My MongoDb collection is as follows</p>
  
  <pre><code>{
      "_id" : ObjectId("5a187babdbf0a03cdca0d0bc"),
      "aggregationDate" : "2017-10-31",
      "ipaddress" : "10.65.66.184",
      "first" : {
          "count" : 3
      },
      "second" : {
          "count" : 2
      },
      "third" : {
          "count" : 3
      },
  }
  
  {
      "_id" : ObjectId("5a187babdbf0a03cdca0d0bd"),
      "aggregationDate" : "2017-10-31",
      "ipaddress" : "10.65.66.182",
      "first" : {
          "count" : 4
      },
      "second" : {
          "count" : 10
      },
      "third" : {
          "count" : 4
      },
  }
  
  {
      "_id" : ObjectId("5a187babdbf0a03cdca0d0be"),
      "aggregationDate" : "2017-10-31",
      "ipaddress" : "10.65.66.189",
      "first" : {
          "count" : 3
      },
      "second" : {
          "count" : 1
      },
      "third" : {
          "count" : 12
      },
  }
  </code></pre>
  
  <p>I want to display the document that has highest sum of count of first, count of second and count of third.</p>
  
  <p>In this case, the output should be -</p>
  
  <pre><code>{
      "_id" : ObjectId("5a187babdbf0a03cdca0d0bd"),
      "aggregationDate" : "2017-10-31",
      "ipaddress" : "10.65.66.182",
      "first" : {
          "count" : 4
      },
      "second" : {
          "count" : 10
      },
      "third" : {
          "count" : 4
      },
  }
  </code></pre>
  
  <p>I only need one document as output.</p>
  
  <pre><code>db.getCollection('foo').aggregate(
  {
      $project: {
          _id: "$ipaddress",
          max: { $max: { $add: [ "$first.count", "$second.count", "$third.count"] } }
          }
   },       
   { $sort: { refCount: -1 }}
   )    
  </code></pre>
  
  <p>I get the following exception</p>
  
  <pre><code>"errmsg" : "exception: invalid operator '$max'"
  </code></pre>
  
  <p>Can someone please help me with this query? Or what i am doing wrong.</p>