title: how to perform mongodb alternate grouping
body: <p>I've a dev_events collection that holds events related to devices, an event has a type and timestamp. The same event type can be received multiple times with different timestamps.
  I want to show for each device, event_type and event_duration by grouping each batch of event_type and calculating date diff (when event starts and when last received before starting a new event type). So, the same event_type may appear multiple times in result but in alternance with other event types.</p>
  
  <p>Here's a sample code I'm using:</p>
  
  <pre><code>db = connect("localhost:27017/dev");                
  
  db.dev_events.insert({"device_id"   : 1, "event_type": 1, "event_timestamp" : new Date(2015,09,17,10,10,10)});
  db.dev_events.insert({"device_id"   : 1, "event_type": 1, "event_timestamp" : new Date(2015,09,17,10,10,40)});
  db.dev_events.insert({"device_id"   : 1, "event_type": 1, "event_timestamp" : new Date(2015,09,17,10,11,10)});
  
  db.dev_events.insert({"device_id"   : 1, "event_type": 2, "event_timestamp" : new Date(2015,09,17,10,11,40)});
  db.dev_events.insert({"device_id"   : 1, "event_type": 2, "event_timestamp" : new Date(2015,09,17,10,12,20)});
  db.dev_events.insert({"device_id"   : 1, "event_type": 2, "event_timestamp" : new Date(2015,09,17,10,12,55)});
  
  db.dev_events.insert({"device_id"   : 1, "event_type": 1, "event_timestamp" : new Date(2015,09,17,10,13,10)});
  db.dev_events.insert({"device_id"   : 1, "event_type": 1, "event_timestamp" : new Date(2015,09,17,10,13,40)});
  db.dev_events.insert({"device_id"   : 1, "event_type": 1, "event_timestamp" : new Date(2015,09,17,10,14,00)});
  
  //map reduce
  var map = function(){
    emit(this.event_type, this);
  }
  
  var reduce = function(key, values){
      var first_happen = values[0].event_timestamp.getTime();
      var last_happen = values[values.length-1].event_timestamp.getTime();
  
      var event_duration = last_happen - first_happen;
      return {event_type : key, duration : event_duration};
  }
  
  var res = db.dev_events.mapReduce(  
                          map, 
                          reduce, 
                          { 
                              query   : {"device_id" : 1},
                              sort    : {"event_timestamp" : 1},
                              out     : {inline : 1} 
                          }
              );
  
  res.find({}).forEach(function(item){ 
      printjson(item); 
  });
  </code></pre>
  
  <p>the output was : </p>
  
  <pre><code>{ "_id" : 1, "value" : { "event_type" : 1, "duration" : 230000 } }
  { "_id" : 2, "value" : { "event_type" : 2, "duration" : 75000 } }
  </code></pre>
  
  <p>when I was expecting event_type 1 to appear twice. Because event 1 happens for a duration, then event 2 happens for a second duration, then again event 1 happens.</p>
  
  <p>So the output should be like this:</p>
  
  <pre><code>{ "_id" : 1, "value" : { "event_type" : 1, "duration" : xxx} }
  { "_id" : 2, "value" : { "event_type" : 2, "duration" : yyy } }
  { "_id" : 1, "value" : { "event_type" : 1, "duration" : zzz } }
  </code></pre>
  
  <p>where xxx, yyy and zzz are durations of events in alternance.</p>
  
  <p>I'm getting confused, I don't know if it's feasable in mongoDB. I'm used to do it in SQL stored procedure without problems.</p>
  
  <p>Any help will be very appreciated,</p>
  
  <p>Regards,</p>