title: How to filter array in a mongodb query
body: <p>In mongodb, I have a collection that contains a single document that looks like the following:</p>
  
  <pre><code>{ 
      "_id" : ObjectId("5552b7fd9e8c7572e36e39df"), 
      "StackSummaries" : [
          {
              "StackId" : "arn:aws:cloudformation:ap-southeast-2:406119630047:stack/XXXX-30fb22a-285-439ee279-c7c8d36/4ebd8770-f8f4-11e4-bf36-503f2370240f", 
              "TemplateDescription" : "XXXX", 
              "StackStatusReason" : "", 
              "CreationTime" : "2015-05-12T22:14:50.535Z", 
              "StackName" : "XXXX", 
              "StackStatus" : "CREATE_COMPLETE"
          }, 
          {
              "TemplateDescription" : "XXXX", 
              "StackStatusReason" : "", 
              "CreationTime" : "2015-05-11T04:02:05.543Z", 
              "StackName" : "XXXX", 
              "StackStatus" : "DELETE_COMPLETE", 
              "StackId" : "arn:aws:cloudformation:ap-southeast-2:406119630047:stack/XXXXX/7c8d04e0-f792-11e4-bb12-506726f15f9a"
          },
          { ... },
          { many others }
      ]
  }
  </code></pre>
  
  <p>ie the imported results of the <strong>aws cli</strong> command <code>aws cloudformation
  list-stacks</code></p>
  
  <p>I'm trying to find the items of the <code>StackSummaries</code> array that have a <code>StackStatus</code> of <code>CREATE_COMPLETE</code> or <code>UPDATE_COMPLETE</code>. After much experimenting and reading other SO posts I arrived at the following:</p>
  
  <pre><code>db.cf_list_stacks.aggregate( {$match: {"StackSummaries.StackStatus": "CREATE_COMPLETE"}})
  </code></pre>
  
  <p>However this still returns the whole document (and I haven't even worried about UPDATE_COMPLETE).</p>
  
  <p>I'm coming from an SQL background and struggling with simple queries like this. Any ideas on how to get the information I'm looking for?</p>
  
  <p>SO posts I've looked at:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/20525754/mongodb-query-with-elemmatch-for-nested-array-data">MongoDB query with elemMatch for nested array data</a></li>
  <li><a href="https://stackoverflow.com/questions/25014699/mongodb-multiple-elemmatch">MongoDB: multiple $elemMatch</a></li>
  <li><a href="https://stackoverflow.com/questions/21519185/projection-vs-elemmatch">$projection vs $elemMatch</a></li>
  <li><a href="https://stackoverflow.com/questions/19696282/make-elemmatch-projection-return-all-objects-that-match-criteria">Make $elemMatch (projection) return all objects that match criteria</a></li>
  </ul>
  
  <p><strong>Update</strong></p>
  
  <p>Notes on things I learned while understanding this topic:</p>
  
  <ul>
  <li><strong>aggregate()</strong> is just a <em>pipeline</em> (like a Unix shell pipeline) where each $ operator is just another step. And like shell pipelines they can look complex, but you just build them up step by step until you get the results you want</li>
  <li>Mongo has a great webinar: <a href="https://www.mongodb.com/webinar/exploring-the-aggregation-framework?jmp=docs&amp;_ga=1.109596165.1669961256.1431482501" rel="nofollow noreferrer">Exploring the Aggregation Framework</a></li>
  <li><a href="http://robomongo.org/" rel="nofollow noreferrer">RoboMongo</a> is a good tool (GPL3) for working with Mongo data and queries</li>
  </ul>