mongodb - how to find and then aggregate

You have to use $match:

db['!all'].aggregate([
  {$match:
    {'GENDER': 'F',
     'DOB':
      { $gte: 19400801,
        $lte: 20131231 } } },
  {$group:
     {_id: "$GENDER",
     totalscore:{ $sum: "$BRAINSCORE"}}}
])

Outputs:

{ "_id" : "F", "totalscore" : 109 }

Sample working query :

db.getCollection('NOTIF_EVENT_RESULT').aggregate([
{$match:
    {'userId': {'$in' : ['user-900', 'user-1546']},
    'criteria.operator': 'greater than', 'criteria.thresold' : '90', 'category' : 'capacity'}
},
{"$group" :  {_id : {userId:"$userId"}, "count" : { "$sum" : 1} } }
])

Tags:

Mongodb