title: Complex MongoDB Aggregation
body: <p>I have a situation where I need to perform a group by operation based on an array value that sums up occurrences of a field value. The counts are then filtered on and the results are prepared so that they can be displayed according to the condition. Essentially, the documents are transformed back to how they would be presented if you simply used the find function. I am running into an issue of the temporary documents being too large due to the number of items collected in the matchedDocuments array. Any suggestions on how to improve this would be helpful.</p>
  
  <pre><code>db.collection1.aggregate([
  {
      '$unwind': '$arrayOfValues'
  }, {
      '$group': {
          '_id': '$arrayOfValues',
          'x_count': {
              $sum: {
                  $cond: [{
                          $eq: ['$field.value', 'x']
                      },
                      1, 0
                  ]
              }
          },
          'y_count': {
              $sum: {
                  $cond: [{
                          $eq: ['$field.value', 'y']
                      },
                      1, 0
                  ]
              }
          },
          'matchedDocuments': {
              '$push': '$$CURRENT'
          }
      }
  },
  {'$match': {'$or': [{'x_count': {'$gte': 2}}, {'y_count': { '$gte': 1}}]}},
  {'$unwind': '$matchedDocuments'},
  {
      '$group': {
          '_id': '$matchedDocuments.key',
          'document': {
              '$last': '$$CURRENT.matchedDocuments'
          }
      }
  }
  ], {
      allowDiskUse: true
  })
  </code></pre>
  
  <p>Below are some sample documents and the expected result based on the criteria above:</p>
  
  <pre><code>// Sample documents
  
  { "_id" : ObjectId("5407c76b7b1c276c74f90524"), "field" : "x", "arrayOfValues" : [ "a", "b", "c" ] }
  { "_id" : ObjectId("5407c76b7b1c276c74f90525"), "field" : "x", "arrayOfValues" : [ "b", "c" ] }
  { "_id" : ObjectId("5407c76b7b1c276c74f90526"), "field" : "z", "arrayOfValues" : [ "a" ] }
  { "_id" : ObjectId("5407c76b7b1c276c74f90527"), "field" : "x", "arrayOfValues" : [ "a", "c" ] }
  { "_id" : ObjectId("5407c76b7b1c276c74f90528"), "field" : "z", "arrayOfValues" : [ "b" ] }
  { "_id" : ObjectId("5407c76b7b1c276c74f90529"), "field" : "y", "arrayOfValues" : [ "k" ] }
  
  
  // Expected Result
  
  [
      { "_id" : ObjectId("5407c76b7b1c276c74f90524"), "field" : "x", "arrayOfValues" : [ "a", "b", "c" ] }
      { "_id" : ObjectId("5407c76b7b1c276c74f90525"), "field" : "x", "arrayOfValues" : [ "b", "c" ] }
      { "_id" : ObjectId("5407c76b7b1c276c74f90527"), "field" : "x", "arrayOfValues" : [ "a", "c" ] }
      { "_id" : ObjectId("5407c76b7b1c276c74f90529"), "field" : "y", "arrayOfValues" : [ "k" ] }
  ]
  </code></pre>