如何在mongoDB中的嵌入字段中获得总和

时间:2018-06-16 01:01:06

标签: mongodb

我有一个mongoDB集合,如下所示:

{u'_id': ObjectId('5b243611dba907285af051ee'),
u'cms_prescription_counts': {u'ABILIFY': 11,
                          u'ALLOPURINOL': 86,
                          u'ALPRAZOLAM': 45,
                          u'AMLODIPINE BESYLATE': 175,
                          u'AMLODIPINE BESYLATE-BENAZEPRIL': 12,
                          u'ATENOLOL': 62,
                          u'ATENOLOL-CHLORTHALIDONE': 53,
                          u'ATORVASTATIN CALCIUM': 19,
                          u'AZITHROMYCIN': 18}},
 {u'_id': ObjectId('5b243611dba90728sad51ee'),
  u'cms_prescription_counts': {u'ABILIFY': 11,
                          u'ALLOPURINOL': 70,
                          u'ALPRAZOLAM': 20,
                          u'AMLODIPINE BESYLATE': 15,
                          u'AMLODIPINE BESYLATE-BENAZEPRIL': 24,
                          u'ATENOLOL': 62,
                          u'ATENOLOL-CHLORTHALIDONE': 53,
                          u'ATORVASTATIN CALCIUM': 19,
                          u'AZITHROMYCIN': 18}
...
...

所以我想在整个系列中获得'ALPRAZOLAM'的总量。我试过$ group如下:

{"$group": {
        "_id": "cms_prescription_counts", 
        "total": {"$sum": "I don't know what to enter here" } 
    }}

任何人都可以帮助我吗? 感谢!!!

4 个答案:

答案 0 :(得分:1)

简单,只需使用汇总管道中的表示法访问嵌入字段

db.collection.aggregate([  
{ "$group" : { 
       "_id": "$cms_prescription_counts", 
      "total" : { "$sum" : "$cms_prescription_counts.ALPRAZOLAM"}
}}
],{allowDiskUse : true})

答案 1 :(得分:0)

要汇总所有ALPRAZOLAM值,您可以使用mapReduce函数:

db.yourCollection.mapReduce(
    function() {
        emit(this.cms_prescription_counts.ALPRAZOLAM); // Mapping each matching document with this item
    },
    function(keyCustId, valuesPrices) {
        return Array.sum(valuesPrices); // Sum all the values
    };
    {
         query: {}, // Filter the collection. Add your condition here
         out: "total" //  This is variable hold the total value
    }
);

答案 2 :(得分:0)

您可以尝试聚合列名:

MongoDB Enterprise > db.test.find().pretty()
{
        "_id" : ObjectId("5b243611dba907285af051ee"),
        "cms_prescription_counts" : {
                "ABILIFY" : 11,
                "ALLOPURINOL" : 86,
                "ALPRAZOLAM" : 45,
                "AMLODIPINE BESYLATE" : 175,
                "AMLODIPINE BESYLATE-BENAZEPRIL" : 12,
                "ATENOLOL" : 62,
                "ATENOLOL-CHLORTHALIDONE" : 53,
                "ATORVASTATIN CALCIUM" : 19,
                "AZITHROMYCIN" : 18
        }
}
{
        "_id" : ObjectId("5b24658a853b142da9b7bd20"),
        "cms_prescription_counts" : {
                "ABILIFY" : 11,
                "ALLOPURINOL" : 70,
                "ALPRAZOLAM" : 20,
                "AMLODIPINE BESYLATE" : 15,
                "AMLODIPINE BESYLATE-BENAZEPRIL" : 24,
                "ATENOLOL" : 62,
                "ATENOLOL-CHLORTHALIDONE" : 53,
                "ATORVASTATIN CALCIUM" : 19,
                "AZITHROMYCIN" : 18
        }
}
MongoDB Enterprise > db.test.aggregate([  { $group : { "_id": "cms_prescription_counts" , "total" : { $sum : "$cms_prescription_counts.ALPRAZOLAM"}}}], { allowDiskUse: true })
{ "_id" : "cms_prescription_counts", "total" : 65 }
MongoDB Enterprise > db.test.aggregate([  { $group : { "_id": "cms_prescription_counts" , "total" : { $sum : "$cms_prescription_counts.ABILIFY"}}}],{ allowDiskUse: true })
{ "_id" : "cms_prescription_counts", "total" : 22 }

您需要添加参数{allowDiskUse : true}。您可以阅读更多相关信息here

使用pymongo时,可以参考question

答案 3 :(得分:0)

您可以尝试:

db.collection.aggregate([
{$project : {'ALPRAZOLAM' : '$cms_prescription_counts.ALPRAZOLAM'}},
{$group:{_id:null,'total' : {$sum : '$ALPRAZOLAM'}}}
])