title: Conditional $first in mongodb aggregation
body: <p>I am trying to figure out if I can somehow combine $first and $ifnull or $cond in a mongodb aggregation method.
  Assuming the following documents:</p>
  
  <pre><code>{ "_id" : 1, "item" : "box" , "code" : null }
  { "_id" : 2, "item" : "box" , "code" : "abcde" }
  { "_id" : 3, "item" : "box" , "code" : "abcde" }
  { "_id" : 4, "item" : "box" , "code" : null }
  </code></pre>
  
  <p>I then run the following aggregation method to group documents together:</p>
  
  <pre><code>db.items.aggregate([{
      $group : {
          _id : '$item',
          code :  { $first : '$code' }
      } 
  }])
  </code></pre>
  
  <p>My aggregation result is:</p>
  
  <pre><code>{ "result" : [ { "_id" : "box", "code" : null } ], "ok" : 1 }
  </code></pre>
  
  <p>I would like to know if there is a way to combine the $first operand with $ifnull or $cond in order to get the code field which is not null. So my result would look like this:</p>
  
  <pre><code>{ "result" : [ { "_id" : "box", "code" : 'abcde' } ], "ok" : 1 }
  </code></pre>
  
  <p>Cheers,</p>