Is there any way to use $filter and $sum in the same $project?

末鹿安然 提交于 2019-12-23 17:43:53

问题


Let's say I have a document in my aggregate pipeline that looks like this:

{
  scores: [
    {type: 'quiz', score: 75},
    {type: 'quiz', score: 62},
    {type: 'final', score: 34},
  ]
}

I'm using $project to transform it, and I'd like to get the sum of the the quiz scores, is there a way I could somehow chain my $filter and $sum.

I know that I could potentially use two $projects, but the way I currently have my pipeline set up would force me to continue reproject a ton of keys in my second project, which I'd like to avoid.


回答1:


YES!

db.collection.aggregate([ 
    { "$project": { 
        "totalQuiz": { 
            "$sum": { 
                "$filter": {
                    "input": "$score", 
                    "as": "s",
                    "cond": { "$eq": [ "$$s.type", "quiz" ] }
                }
             }
        }
    }
])



回答2:


You would need help from another operator that maps the scores embedded documents into an array of values that you can $sum. You can do that using the $map operator as in the following:

db.test.aggregate([ 
    { 
        "$project": { 
            "scores": 1,
            "quiz_total": { 
                "$sum": {
                    "$map": { 
                        "input": "$scores", 
                        "as": "item", 
                        "in": { 
                            "$cond": [ 
                                { "$eq": [ "$$item.type", "quiz" ] }, 
                                "$$item.score", 
                                0 
                            ]
                        }
                    }
                }                
            }    
        }   
    }
])

Sample Output

{
    "_id" : ObjectId("582ac9619602e37d2794acd3"),
    "scores" : [ 
        {
            "type" : "quiz",
            "score" : 75
        }, 
        {
            "type" : "quiz",
            "score" : 62
        }, 
        {
            "type" : "final",
            "score" : 34
        }
    ],
    "quiz_total" : 137
}


来源:https://stackoverflow.com/questions/40601495/is-there-any-way-to-use-filter-and-sum-in-the-same-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!