mongoDb字段计数查询

时间:2018-08-10 02:04:31

标签: mongodb mongodb-query aggregation-framework

我有以下格式的MongoDB集合:

{
    "_id" : ObjectId("5b6b112d1bbd972848b97df4"),
    "Game" : [
            {
                    "City" : "Nizhny Novgorod",
                    "T2N" : "Costa Rica",
                    "T1N" : "Switzerland",
                    "ST" : "Nizhny Novgorod Stadium",
                    "T1S" : "2",
                    "Date" : "6/27/2018",
                    "T2S" : "2"
            },
            {
                    "City" : "Kaliningrad",
                    "T2N" : "Serbia",
                    "T1N" : "Switzerland",
                    "ST" : "Kaliningrad Stadium",
                    "T1S" : "2",
                    "Date" : "6/22/2018",
                    "T2S" : "1"
            },
            {
                    "City" : "Rostov-on-Don",
                    "T2N" : "Brazil",
                    "T1N" : "Switzerland",
                    "ST" : "Rostov Arena",
                    "T1S" : "1",
                    "Date" : "6/17/2018",
                    "T2S" : "1"
            },
            {
                    "City" : "Saint Petersburg",
                    "T2N" : "Sweden",
                    "T1N" : "Switzerland",
                    "ST" : "Saint Petersburg Stadium",
                    "T1S" : "0",
                    "Date" : "7/3/2018",
                    "T2S" : "1"
            }
    ],
    "team" : "Switzerland"
}

如何获取每个团队的T1S总数和T2S总数? 我正在尝试以下查询

db.test3.aggregate([{$match:{}},{$group:{_id:"$team", goalScored:{$sum:"Game.T1S"},goalConceaded:{$sum:"Game.T2S"}}}])

但是我得到以下输出:

{ "_id" : "Tunisia", "goalScored" : 0, "goalConceaded" : 0 }
{ "_id" : "Uruguay", "goalScored" : 0, "goalConceaded" : 0 }
{ "_id" : "Egypt", "goalScored" : 0, "goalConceaded" : 0 }

在上面,目标得分和目标得分始终为0,这是不期望的。任何帮助表示赞赏。

0 个答案:

没有答案