title: mongodb - how to find and then aggregate
body: <p>I have collection that contains documents with below schema. I want to filter/find all documents that contain the gender female and aggregate the sum of brainscore. I tried the below statement and it shows a invalid pipeline error.</p>
  <pre><code>db['!all'].aggregate({ $and: [ {'GENDER' :  'F'} , {'DOB' : { $gte : 19400801, $lte : 20131231 }} ]  }, { $group : { _id : &quot;$GENDER&quot;, totalscore : { $sum : &quot;$BRAINSCORE&quot; } } } )
  </code></pre>
  <p>Schema:</p>
  <pre><code>{
      &quot;_id&quot; : ObjectId(&quot;53f63fc8f2b643f6ebb8a1a9&quot;),
      &quot;DOB&quot; : 19690112,
      &quot;GENDER&quot; : &quot;F&quot;,
      &quot;BRAINSCORE&quot; : 65
  },
  {
      &quot;_id&quot; : ObjectId(&quot;53f63fc8f2b643f6ebb8a1a2&quot;),
      &quot;DOB&quot; : 19950116,
      &quot;GENDER&quot; : &quot;F&quot;,
      &quot;BRAINSCORE&quot; : 44
  },
  {
      &quot;_id&quot; : ObjectId(&quot;53f63fc8f2b643f6ebb8a902&quot;),
      &quot;DOB&quot; : 19430216,
      &quot;GENDER&quot; : &quot;M&quot;,
      &quot;BRAINSCORE&quot; : 71
  }
  </code></pre>