【问题标题】:how to get the sum in embedded fields in mongoDB如何在 mongoDB 的嵌入式字段中获取总和
【发布时间】:2018-11-25 19:23:53
【问题描述】:

我有一个 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" } 
    }}

有人可以帮助我吗? 谢谢!!!

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    简单,只需在聚合管道中使用 . 表示法访问嵌入字段

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

    【讨论】:

    • 它有错误超过 $group 的内存限制,但不允许外部排序。通过 allowDiskUse:true 选择加入。
    • 我刚刚在同一个查询中添加了 ,{allowDiskUse : true},您现在可以查看
    【解决方案2】:

    要对所有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
        }
    );
    

    【讨论】:

      【解决方案3】:

      你可以试试:

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

      【讨论】:

        【解决方案4】:

        您可以尝试对列名进行聚合:

        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

        【讨论】:

        • 我试过 pipeline=[ { "$group" : { "_id": "$cms_prescription_counts", "total" : { "$sum" : "$cms_prescription_counts.ALPRAZOLAM"} }} ] db .pre_collection.aggregate(pipeline) 但出现错误
        • 你能分享一下错误吗?它对我来说工作正常。
        • 错误是 Exceeded memory limit for $group,但不允许外部排序。通过 allowDiskUse:true 选择加入。顺便说一句,我使用 pymongo
        • 基本上需要在聚合管道中添加 {allowDiskUse : true }。设置为开启时,这允许 mongo 在磁盘上写入临时文件。
        猜你喜欢
        • 2020-05-01
        • 1970-01-01
        • 2022-11-24
        • 2023-01-13
        • 2016-05-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-02
        相关资源
        最近更新 更多