使用MongoDB中的聚合框架按特定嵌套字段值计算文档

时间:2016-08-01 07:22:11

标签: mongodb mongodb-query aggregation-framework

这是我的数据库。

{
"_id" : ObjectId("579cab6c6aba30f42a57a979"),
"iecode" : "P1111",
"country" : "India",
"totalTreatmentArms" : 3,
"treatmentArms" : [ 
    {
        "mechanismOrPkg" : "Mechanism",
        "mechanism" : "mechanism1"
    }, 
    {
        "mechanismOrPkg" : "Mechanism",
        "mechanism" : "mechanism2"
    }, 
    {
        "mechanismOrPkg" : "Package",
        "mechanism" : "mechanism1"
    }
]
}


{
"_id" : ObjectId("579cab7a6aba30f42a57a97a"),
"iecode" : "P1111",
"country" : "Canada",
"totalTreatmentArms" : 3,
"treatmentArms" : [ 
    {
        "mechanismOrPkg" : "Mechanism",
        "mechanism" : "mechanism1"
    }, 
    {
        "mechanismOrPkg" : "Mechanism",
        "mechanism" : "mechanism2"
    }, 
    {
        "mechanismOrPkg" : "Package",
        "mechanism" : ""
    }
]
}


{
"_id" : ObjectId("579cabac6aba30f42a57a97b"),
"iecode" : "P2222",
"country" : "India",
"totalTreatmentArms" : 1,
"treatmentArms" : [ 
    {
        "mechanismOrPkg" : "Package",
        "mechanism" : ""
    }
]
}


{
"_id" : ObjectId("579cabe76aba30f42a57a97c"),
"iecode" : "P3333",
"country" : "India",
"totalTreatmentArms" : 2,
"treatmentArms" : [ 
    {
        "mechanismOrPkg" : "Mechanism",
        "mechanism" : "mechanism1"
    }, 
    {
        "mechanismOrPkg" : "Package",
        "mechanism" : ""
    }
]
}

请注意,iecode : P1111有两条记录,我希望iecode区分结果,因此只会考虑一条记录(任何一条记录)。

现在我的要求是我想依靠字段mechanismOrPkg。如果它包含值Package,那么我们将使用一个增加package。如果值为Mechanism,那么我们将考虑字段mechanism的值,并考虑其所尊重的值计数。

所以最后的结果将是这样的

{
"_id" : null,
"totalPackage" : 3,
"totalMechanism1" : 2,
"totalMechanism2" : 1
}

请问这听起来有点令人困惑。即使这种聚合可以通过查询进行,或者我必须进行服务器端过滤,请告诉我?

感谢。

修改

mechanismOrPkg的可能值:包或机制

机制的可能值:mechanism1或mechanism2

1 个答案:

答案 0 :(得分:3)

要完成上述操作,需要在$cond累加器运算符中使用 $sum 运算符。 $cond 运算符将根据其第一个参数(if)计算逻辑条件,然后返回第二个参数,其中评估为true(然后)或第三个参数,其中false(else) 。这会将true/false逻辑转换为分别输入$sum的1和0数值。因此,例如,当您想要聚合"包"价值,逻辑如下:

{
    "$sum": { 
        "$cond": [ { "$eq": [ "$treatmentArms.mechanismOrPkg", "Package" ] }, 1, 0 ]
    }
}

作为生成的管道,您需要运行聚合操作

db.collection.aggregate([
    {
        "$group": {
            "_id": "$iecode",
            "treatmentArms": { "$first": "$treatmentArms" }
        }
    },
    { "$unwind": "$treatmentArms" },
    {
        "$group": {
            "_id": null,
            "totalPackage": { 
                "$sum": { 
                   "$cond": [ 
                       { "$eq": [ "$treatmentArms.mechanismOrPkg", "Package" ] }, 
                       1, 0
                    ] 
                }
            },
            "totalMechanism1":{
                "$sum": { 
                   "$cond": [ 
                        { 
                            "$and": [
                                { "$eq": [ "$treatmentArms.mechanismOrPkg", "Mechanism" ] },
                                { "$eq": [ "$treatmentArms.mechanism", "mechanism1" ] }
                            ]
                        }, 
                        1, 
                        0 ]
                }
            },
            "totalMechanism2": { 
                "$sum": { 
                   "$cond": [ 
                        { 
                            "$and": [
                                { "$eq": [ "$treatmentArms.mechanismOrPkg", "Mechanism" ] },
                                { "$eq": [ "$treatmentArms.mechanism", "mechanism2" ] }
                            ]
                        }, 
                        1, 
                        0 ]
                }
            }
        }
    }
])

示例输出

{
    "_id" : null,
    "totalPackage" : 3,
    "totalMechanism1" : 2,
    "totalMechanism2" : 1
}