如何查询mongodb中的多级子文档以获取字段的$ sum?

时间:2013-10-18 15:29:20

标签: mongodb

以下是“Page”集合中的一些文档,我想获得字段“memberProfile_editAccountProfile”的总和?

{
   "_id" : NumberLong(2013083117),
   "count" : 3000,
   "requests" : {
       "consumermx" : {            
       "NFCU" : {
               "memberDashboard_editDashboard" : 24,
               "memberProfile_editAccountProfile" : 1,
               "memberProfile_editPreferencesAssociation" : 1,
               "memberProfile_editPromotionReminder" : 59,
               "memberProfile_savePromotionReminder" : 6,
                        }
                      }
                 }
}

{
   "_id" : NumberLong(2013083118),
   "count" : 3000,
   "requests" : {
       "consumermx" : {            
       "NFCU" : {
               "memberDashboard_editDashboard" : 12,
               "memberProfile_editAccountProfile" : 23,
               "memberProfile_editPreferencesAssociation" : 6,
               "memberProfile_editPromotionReminder" : 21,
               "memberProfile_savePromotionReminder" : 9,
                        }
                      }
                 }
}

1 个答案:

答案 0 :(得分:0)

这个怎么样?

db.Page.aggregate ([{"$group": { "_id":null,
                                 "editAccountProfileSum": {"$sum":"$requests.consumermx.NFCU.memberProfile_editAccountProfile"}}
                     }])