mongodb - how to find and then aggregate

前端 未结 3 1372
忘了有多久
忘了有多久 2021-02-01 11:39

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

3条回答
  •  时光取名叫无心
    2021-02-01 12:25

    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 }
    

提交回复
热议问题