首页 > 解决方案 > 我想通过聚合对mongo中的count字段求和,数组是动态的,格式是spend_imp->today_date(everyday new array gen,)->count.......)

问题描述

    array = "spend_imp" : {
              "2019-11-19" : {
                  "created" : 1574167415264.0,
                  "count" : 1
              },
              "2019-11-20" : {
                  "count" : 4,
                  "created" : 1574229189457.0
              }
          }
    try {
        Main_add_list.aggregate( [
            { $match: {"add_id" : data.add_id }  },
            {$project:{
                "_id":1,
                "spend_imp":1,
                "Total_spend":{$sum:"$spend_imp.2019-11-19"},
            }},
            ] ).then((result) => {
                    return callback(result);        
                }).catch((err) =>{
                    console.log(err);
                    return callback(err);
                })
    }
    catch(e){ return callback(null); }

我动态地制作数组。

数组格式为-> today_date( gen.spend_imp下每天新的嵌套数组) -> today_date 中的字段(例如,2019 年 11 月 20 日)。所以我想总结一下。例如,我有 2 个数组和,所以我想要两个数组的总和。spend_impcountcount11/19/201911/20/2019

标签: node.jsmongodb

解决方案


推荐阅读