title: combine multiple match() results in mongodb aggregation framework
body: <p>I got the following query:</p>
  
  <pre><code>db.getCollection('Messages').aggregate(
      { 
      $match : 
          { 
              "header.MessageType" : { $in: ["04"] },
          }
  
      },
      {
          $project:
            {
               _id: '$_id',
               header: '$header',
               content: '$content',
               q0: { $arrayElemAt: [ "$content.Changes", 0 ] },
               q1: { $arrayElemAt: [ "$content.Changes", 1 ] },
               q2: { $arrayElemAt: [ "$content.Changes", 2 ] },
               q3: { $arrayElemAt: [ "$content.Changes", 3 ] },
               q4: { $arrayElemAt: [ "$content.Changes", 4 ] },
               q5: { $arrayElemAt: [ "$content.Changes", 5 ] },
            }
      },
      {
          $match : 
          { 
              "q0":"1"
          }
      },
      { 
          $sort : { "sequenceID" : -1, } 
      },
      { 
          $limit : 1
      }
  );
  </code></pre>
  
  <p>which gives me the following results:</p>
  
  <pre><code>/* 1 */
  {
      "_id" : ObjectId("57288ecb53f65928c4ba4995"),
      "header" : {
          "MessageType" : "04",
          ...
      },
      "content" : {
          ...
      },
      "q0" : "1",
      "q1" : "0",
      "q2" : "1",
      "q3" : "0",
      "q4" : "0",
      "q5" : "0"
  </code></pre>
  
  <p>the query gives me the most recent document (according to sequenceID) which its 'q0' is definitely  equals to 1 (other qx fields might be 0 or 1). 
  I want to do the same for q1,q2,...,q5. In other words, I want a result containing 6 records each of which representing the most recent document that its 'qn' (n=0..5) record is definitely '0'.</p>
  
  <p>is it possible to repeat the following part 6 times (for different qx values in $match section) and then combine them?</p>
  
  <pre><code>{
      $match : 
      { 
          "q0":"1"
      }
  },
  { 
      $sort : { "sequenceID" : -1, } 
  },
  { 
      $limit : 1
  }
  </code></pre>
  
  <p>Or is there any better solution for this?</p>
  
  <p>--- EDIT (some sample [and simplified] data added):</p>
  
  <pre><code>/* 1 */
  {
      "_id" : ObjectId("57288fa553f65928c4bf4b2b"),
      "header" : {
          "MessageType" : "04"
      },
      "content" : {
          "Changes" : [ 
              "0", 
              "1", 
              "1", 
              "1", 
              "1", 
              "0"
          ]
      },
      "sequenceID" : NumberLong(369851),
      "messageDate" : 13950214
  }
  
  /* 2 */
  {
      "_id" : ObjectId("57288fa453f65928c4bf4863"),
      "header" : {
          "MessageType" : "04"
      },
      "content" : {
          "Changes" : [ 
              "0", 
              "0", 
              "1", 
              "0", 
              "0", 
              "0"
          ]
      },
      "sequenceID" : NumberLong(369139),
      "messageDate" : 13950214
  }
  
  /* 3 */
  {
      "_id" : ObjectId("57288fa353f65928c4bf43c2"),
      "header" : {
          "MessageType" : "04"
      },
      "content" : {
          "Changes" : [ 
              "0", 
              "1", 
              "0", 
              "0", 
              "0", 
              "0"
          ]
      },
      "sequenceID" : NumberLong(367953),
      "messageDate" : 13950214
  }
  </code></pre>
  
  <p>Each document indicates one or more (up to 6) modifications. The modifications are seen in the q0..q5 fields. What I need as the output is the most recent changes for each qx field. the order of documents can be determined by the "sequenceID" field. In other words if I (logically do an) OR the q0..q5 of all documents in the result, it should be ["1","1","1","1","1","1"].</p>