title: MongoDB Aggregation - $project with $cond
body: <p>I'm trying to use MongoDBs (v.3.2.11) aggregation framework to process some log documents that look like the following:</p>
  
  <pre><code>{ 
      "_id" : ObjectId("58b753c6d4421f00216de942"), 
      "session_id" : "7CB8725A-3994-45B8-9CA2-92FC19406288", 
      "event_type" : "connect_begin", 
      "timestamp" : "1488409541.674997", 
      "user_id" : "f6830aac-60be-44df-9fa7-7aa530d637ce", 
      "u_at" : ISODate("2017-03-01T23:05:42.077Z"), 
      "c_at" : ISODate("2017-03-01T23:05:42.077Z") 
  }
  </code></pre>
  
  <p>My collection contains pairs of the above logs that share a <code>session_id</code>, one log for the <code>begin</code> event and one for the <code>end</code> event. The end goal is to calculate the length of these sessions via the difference in the timestamps.</p>
  
  <p>So far I have been able to write an aggregation pipeline that groups the logs by <code>$session_id</code> and provides an array of the two <code>$events</code> associated with the session. My idea was that next I would <code>$project</code> the begin and end timestamps onto the final result using a <code>$cond</code> to check the <code>event_type</code> of each <code>$event</code> in the array, which will tell me if it is a <code>begin</code> or <code>end</code> event. I have pasted what I have so far below:</p>
  
  <pre><code>db.time_spent_logs.aggregate([
      { $group: {
              _id: '$session_id',
              events: {
                  $push: {
                      event_type: '$event_type', 
                      timestamp: '$timestamp'
              }
          }
      }}, 
      { $project: {
          start: {
              $cond: { 
                  if: { $or: [ { $strcasecmp: [ "$events[0].event_type", "trending_begin" ]}, { $strcasecmp: [ "$events[0].event_type", "connect_begin" ]}] },
                  then: '$events[0].timestamp', 
                  else: '$events[1].timestamp'
              }
          },
          end: {
              $cond: {
                  if: { $or: [ { $strcasecmp: [ "$events[0].event_type", "trending_end" ]}, { $strcasecmp: [ "$events[0].event_type", "connect_end" ]}] },
                  then: '$events[0].timestamp', 
                  else: '$events[1].timestamp'
  
              }
          }
      }}
  ])
  </code></pre>
  
  <p>This produces a list of the following:</p>
  
  <pre><code>{ "_id" : "4EC4B831-D3C7-49C6-9EC8-301981639ED7" }
  </code></pre>
  
  <p>I think my problem is in the <code>if</code> of my <code>$cond</code>, where I am comparing the value of the <code>event_type</code> field for each <code>$event</code> with a string to see if it is one of our two <code>begin</code> or <code>end</code> event types. I believe it is somewhere in this <code>$if $or $strcasecompare</code> where I have something wrong...</p>
  
  <p>I have attempted using <code>$literal</code> to compare the <code>event_type</code> as well to no result.</p>
  
  <p>Any help would be very much appreciated!</p>