mongodb子文档分组统计

新手上路,请多包涵

有如下数据、要怎样才能根据 detail.code 分组来统计到 detail.number 的总和呢?即 sum(detail.number)

{
    "_id" : "b07022a5159c4076a905f31965a04bda",
    "user_id" : "16b6716ff6f24163a7a875543f16603e",
    "scope_id" : 5,
    "data" : {
        "unit_principal" : "123",
        "count_principal" : "123",
        "write_person" : "123",
        "contact_number" : "123",
        "report_time" : "2020-02-05"
    },
    "detail" : [ 
        {
            "is_title" : 1,
            "type" : 1,
            "indentation" : 1,
            "name" : "一、研发开发人员情况",
            "unit" : "0",
            "code" : "0",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 1,
            "indentation" : 2,
            "name" : "研究开发人员合计",
            "unit" : "人",
            "code" : "1",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 1,
            "indentation" : 3,
            "name" : "其中:管理和服务人员",
            "unit" : "人",
            "code" : "2",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 1,
            "indentation" : 3,
            "name" : "其中:女性",
            "unit" : "人",
            "code" : "3",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 1,
            "indentation" : 3,
            "name" : "其中:全职人员",
            "unit" : "人",
            "code" : "4",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 1,
            "indentation" : 3,
            "name" : "其中:本科毕业及以上人员",
            "unit" : "人",
            "code" : "5",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 1,
            "indentation" : 3,
            "name" : "其中:外聘人员",
            "unit" : "人",
            "code" : "6",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 1,
            "type" : 2,
            "indentation" : 1,
            "name" : "二、研发开发人员情况",
            "unit" : "0",
            "code" : "0",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 2,
            "indentation" : 2,
            "name" : "研究开发费用合计",
            "unit" : "千元",
            "code" : "7",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 2,
            "indentation" : 3,
            "name" : "人员人工费用",
            "unit" : "千元",
            "code" : "8",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 2,
            "indentation" : 3,
            "name" : "直接投入费用",
            "unit" : "千元",
            "code" : "9",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 2,
            "indentation" : 3,
            "name" : "折旧费用与长期推摊费用",
            "unit" : "千元",
            "code" : "10",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 2,
            "indentation" : 3,
            "name" : "无形资产推销费用",
            "unit" : "千元",
            "code" : "11",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 2,
            "indentation" : 3,
            "name" : "设计费用",
            "unit" : "千元",
            "code" : "12",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 2,
            "indentation" : 3,
            "name" : "装备调试费用与试验费用",
            "unit" : "千元",
            "code" : "13",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 2,
            "indentation" : 3,
            "name" : "委托外部研究开发费用",
            "unit" : "千元",
            "code" : "14",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 3,
            "indentation" : 4,
            "name" : "①委托境内研究机构",
            "unit" : "千元",
            "code" : "15",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 2,
            "indentation" : 4,
            "name" : "②委托境内高等学校",
            "unit" : "千元",
            "code" : "16",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 2,
            "indentation" : 4,
            "name" : "③委托境内企业",
            "unit" : "千元",
            "code" : "17",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 2,
            "indentation" : 4,
            "name" : "④委托境外机构",
            "unit" : "千元",
            "code" : "18",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 2,
            "indentation" : 3,
            "name" : "其他费用",
            "unit" : "千元",
            "code" : "19",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 1,
            "type" : 3,
            "indentation" : 1,
            "name" : "三、研究开发资产情况",
            "unit" : "0",
            "code" : "0",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 3,
            "indentation" : 2,
            "name" : "当年形成用于研究开发的固定资产",
            "unit" : "千元",
            "code" : "20",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 3,
            "indentation" : 3,
            "name" : "其中:仪器和设备",
            "unit" : "千元",
            "code" : "21",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 1,
            "type" : 4,
            "indentation" : 1,
            "name" : "四、企业办研究开发机构(境内)情况",
            "unit" : "0",
            "code" : "0",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 4,
            "indentation" : 2,
            "name" : "期末机构数",
            "unit" : "个",
            "code" : "22",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 4,
            "indentation" : 2,
            "name" : "机构研究开发人员",
            "unit" : "人",
            "code" : "23",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 4,
            "indentation" : 3,
            "name" : "其中:博士毕业",
            "unit" : "人",
            "code" : "24",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 4,
            "indentation" : 3,
            "name" : "其中:硕士毕业",
            "unit" : "人",
            "code" : "25",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 4,
            "indentation" : 2,
            "name" : "机构研究开发费用",
            "unit" : "千元",
            "code" : "26",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 4,
            "indentation" : 2,
            "name" : "期末仪器和设备原价",
            "unit" : "千元",
            "code" : "27",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 4,
            "indentation" : 3,
            "name" : "其中:进口",
            "unit" : "千元",
            "code" : "28",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 1,
            "type" : 5,
            "indentation" : 1,
            "name" : "五、研究开发产出及相关情况",
            "unit" : "0",
            "code" : "0",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 1,
            "type" : 5,
            "indentation" : 1,
            "name" : "(一)专利情况",
            "unit" : "0",
            "code" : "0",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 5,
            "indentation" : 2,
            "name" : "当年专利申请数",
            "unit" : "件",
            "code" : "29",
            "number" : 1.0,
            "list" : [ 
                "baa5ea919f1d4780a00f1e95f709a93c"
            ],
            "list_patent" : [ 
                {
                    "_id" : "baa5ea919f1d4780a00f1e95f709a93c",
                    "user_id" : "16b6716ff6f24163a7a875543f16603e",
                    "scope_id" : 5,
                    "unique_id" : "11232123",
                    "name" : "伪强翁",
                    "type" : "实用型专利",
                    "apply_person" : "打",
                    "apply_time" : "2020-01-09",
                    "summary" : "add撒撒",
                    "picture" : "/16b6716ff6f24163a7a875543f16603e/4a41993d353912ad4bacd22b3e47c44d1577979268776.png",
                    "authenticate_status" : 0,
                    "del_status" : 1,
                    "insert_time" : NumberLong(1577979358017),
                    "update_time" : NumberLong(1577979358017)
                }
            ],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 5,
            "indentation" : 3,
            "name" : "其中:发明专利",
            "unit" : "件",
            "code" : "30",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 5,
            "indentation" : 3,
            "name" : "其中:PTC专利",
            "unit" : "件",
            "code" : "31",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 5,
            "indentation" : 2,
            "name" : "期末有效发明专利数",
            "unit" : "件",
            "code" : "32",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 5,
            "indentation" : 3,
            "name" : "其中:已被实施",
            "unit" : "件",
            "code" : "33",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 5,
            "indentation" : 3,
            "name" : "其中:境外授权",
            "unit" : "件",
            "code" : "34",
            "number" : 1.0,
            "list" : [ 
                "baa5ea919f1d4780a00f1e95f709a93c"
            ],
            "list_patent" : [ 
                {
                    "_id" : "baa5ea919f1d4780a00f1e95f709a93c",
                    "user_id" : "16b6716ff6f24163a7a875543f16603e",
                    "scope_id" : 5,
                    "unique_id" : "11232123",
                    "name" : "伪强翁",
                    "type" : "实用型专利",
                    "apply_person" : "打",
                    "apply_time" : "2020-01-09",
                    "summary" : "add撒撒",
                    "picture" : "/16b6716ff6f24163a7a875543f16603e/4a41993d353912ad4bacd22b3e47c44d1577979268776.png",
                    "authenticate_status" : 0,
                    "del_status" : 1,
                    "insert_time" : NumberLong(1577979358017),
                    "update_time" : NumberLong(1577979358017)
                }
            ],
            "list_copyright" : []
        }, 
        {
            "is_title" : 1,
            "type" : 5,
            "indentation" : 1,
            "name" : "(二)新产品情况",
            "unit" : "0",
            "code" : "0",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 5,
            "indentation" : 2,
            "name" : "新产品产值",
            "unit" : "千元",
            "code" : "35",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 5,
            "indentation" : 2,
            "name" : "新产品销售收入",
            "unit" : "千元",
            "code" : "36",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 5,
            "indentation" : 3,
            "name" : "其中:出口",
            "unit" : "千元",
            "code" : "37",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 1,
            "type" : 5,
            "indentation" : 1,
            "name" : "(三)其他情况",
            "unit" : "0",
            "code" : "0",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 5,
            "indentation" : 2,
            "name" : "期末拥有注册商标",
            "unit" : "件",
            "code" : "38",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 5,
            "indentation" : 3,
            "name" : "其中:境外注册",
            "unit" : "件",
            "code" : "39",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 5,
            "indentation" : 2,
            "name" : "发表科技论文",
            "unit" : "篇",
            "code" : "40",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 5,
            "indentation" : 2,
            "name" : "形成国家或行业标准",
            "unit" : "项",
            "code" : "41",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 5,
            "indentation" : 2,
            "name" : "软件著作权",
            "unit" : "个",
            "code" : "42",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 1,
            "type" : 6,
            "indentation" : 1,
            "name" : "六、其他相关情况",
            "unit" : "0",
            "code" : "0",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 1,
            "type" : 6,
            "indentation" : 1,
            "name" : "(一)政府经费及相关政策落实情况",
            "unit" : "0",
            "code" : "0",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 6,
            "indentation" : 2,
            "name" : "来自政府部门的研究开发经费",
            "unit" : "千元",
            "code" : "43",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 6,
            "indentation" : 2,
            "name" : "研究开发费用加计扣除减免税",
            "unit" : "千元",
            "code" : "44",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 6,
            "indentation" : 2,
            "name" : "高新技术企业减免税",
            "unit" : "千元",
            "code" : "45",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 1,
            "type" : 6,
            "indentation" : 1,
            "name" : "(二)技术改造和技术情趣情况",
            "unit" : "0",
            "code" : "0",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 6,
            "indentation" : 2,
            "name" : "技术改造经费支出",
            "unit" : "千元",
            "code" : "46",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 6,
            "indentation" : 2,
            "name" : "购买境内技术经费支出",
            "unit" : "千元",
            "code" : "47",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 6,
            "indentation" : 2,
            "name" : "引进境外技术经费支出",
            "unit" : "千元",
            "code" : "48",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 6,
            "indentation" : 2,
            "name" : "引进境外技术的笑话吸收经费",
            "unit" : "千元",
            "code" : "49",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 1,
            "type" : 6,
            "indentation" : 1,
            "name" : "(三)企业办研究开发机构(境外)情况",
            "unit" : "0",
            "code" : "0",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 6,
            "indentation" : 2,
            "name" : "期末企业在境外设立的研究开发机构数",
            "unit" : "个",
            "code" : "50",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 1,
            "type" : 7,
            "indentation" : 1,
            "name" : "七、补充资料",
            "unit" : "0",
            "code" : "0",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 7,
            "indentation" : 2,
            "name" : "上年研究开发费用合计",
            "unit" : "千元",
            "code" : "51",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }, 
        {
            "is_title" : 0,
            "type" : 7,
            "indentation" : 3,
            "name" : "其中:已加计扣除的研究开发费用",
            "unit" : "千元",
            "code" : "52",
            "number" : 0.0,
            "list" : [],
            "list_patent" : [],
            "list_copyright" : []
        }
    ],
    "authenticate_status" : 0,
    "del_status" : 1,
    "insert_time" : NumberLong(1577996366686),
    "update_time" : NumberLong(1577996366686)
}
阅读 1.9k
1 个回答
✓ 已被采纳新手上路,请多包涵

问题解决

List<AggregationOperation> aggs = new ArrayList<>();  
aggs.add(Aggregation.unwind("detail"));  
aggs.add(Aggregation.match(Criteria.where("authenticate_status").is(0)  
        .and("scope_id").is(adminScopeId)  
        .and("detail.is_title").is(0)));  
aggs.add(Aggregation.project()  
        .and("detail.is_title").as("is_title")  
        .and("detail.type").as("type")  
        .and("detail.indentation").as("indentation")  
        .and("detail.name").as("name")  
        .and("detail.unit").as("unit")  
        .and("detail.code").as("code")  
        .and("detail.number").as("number"));  
aggs.add(Aggregation.group("code")  
        .first("is_title").as("is_title")  
        .first("type").as("type")  
        .first("indentation").as("indentation")  
        .first("name").as("name")  
        .first("unit").as("unit")  
        .first("code").as("code")  
        .sum("number").as("number"));  
aggs.add(Aggregation.sort(Sort.by(Sort.Order.asc("code"))));  
Aggregation agg = Aggregation.newAggregation(aggs);  
AggregationResults<Tmp> results = mongodb.aggregate(agg,"a_project_activity_lib",Tmp.class);

参考了这文章spring-data mongodb aggregate 按照子文档属性分组用法

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进