如何使用聚合管道运算符构建嵌套保护

时间:2019-05-28 13:10:29

标签: java mongodb spring-data

我正在尝试使用spring-data-mongodb构建聚合,但是我对“项目”有一些疑问,我不知道如何将mongo shell转换为Java代码。

我正在使用 spring-data-mongodb-2.1.4.RELEASE 软件包

{ 
    "_id" : ObjectId("5ceccfbebd839e0e606e2d28"), 
    "deptId" : "103", 
    "deptName" : "deptA", 
    "pId" : "1", 
    "pIds" : [
        "1"
    ], 
    "history" : {
        "201905" : {
            "createInfoStaffNum" : 12.0, 
            "goldInfoStaffNum" : 1.0, 
            "receiveClueNum" : 12.0, 
            "usefulClueNum" : 4.0, 
            "dealingDubanNum" : 5.0, 
            "receiveDubanNum" : 12.0, 
            "additionalScore" : 12.3
        }
    }, 
    "level" : NumberInt(1), 
    "juzhiDepartment" : false, 
    "_class" : "com.gzjp.core.base.vo.document.DeptStatisticsDocument"
}

{ 
    "_id" : ObjectId("5ceccfbebd839e0e606e2cb0"), 
    "deptId" : "104", 
    "deptName" : "deptB", 
    "pId" : "103", 
    "pIds" : [
        "1", 
        "103"
    ], 
    "level" : NumberInt(2), 
    "juzhiDepartment" : false, 
    "history" : {
        "201905" : {
            "createInfoStaffNum" : 23.0, 
            "goldInfoStaffNum" : 1.0, 
            "receiveClueNum" : 12.0, 
            "usefulClueNum" : 4.0, 
            "dealingDubanNum" : 5.0, 
            "receiveDubanNum" : 12.0, 
            "additionalScore" : 12.3
        }
    }, 
    "_class" : "com.gzjp.core.base.vo.document.DeptStatisticsDocument"
}

  

这是集合数据结构,有我的数据类型链接Model Tree Structures with an Array of Ancestors

db.getCollection("auditScore").aggregate(
    [
        { 
            "$match" : {
                "level" : 1.0, 
                "juzhiDepartment" : false
            }
        }, 
        { 
            "$graphLookup" : {
                "from" : "auditScore", 
                "startWith" : "$deptId", 
                "connectFromField" : "deptId", 
                "connectToField" : "pIds", 
                "depthField" : "level", 
                "as" : "children"
            }
        }, 
        { 
            "$project" : {
                "deptId" : 1.0, 
                "deptName" : 1.0, 
                "pId" : 1.0, 
                "pIds" : 1.0, 
                "_class" : 1.0, 
                "childNames" : "$children", 
                "statistics" : {
                    "createInfoStaffNum" : {
                        "$ifNull" : [
                            {
                                "$add" : [
                                    {
                                        "$sum" : "$children.history.201905.createInfoStaffNum"
                                    }, 
                                    "$history.201905.createInfoStaffNum"
                                ]
                            }, 
                            0.0
                        ]
                    }
                }
            }
        }
    ], 
    { 
        "allowDiskUse" : false
    }
);
  

这是我的mongo shell代码,但我不知道如何将其转换为Java代码,尤其是在“统计”中。

TypedAggregation<DeptStatisticsDocument> aggregation = newAggregation(DeptStatisticsDocument.class,
                match(Criteria.where("level").is(1).and("juzhiDepartment")),
                graphLookup(collectionName)
                        .startWith("$deptId")
                        .connectFrom("deptId")
                        .connectTo("pIds")
                        .depthField("level")
                        .as("children"),
                project("deptId", "deptName", "pId", "pIds", "level", "juzhiDepartment")
                        .and("statistics")???

        );
  

我对本节感到困惑。

0 个答案:

没有答案