如何从MongoDB中的不同文档聚合数组值?

时间:2018-03-01 03:19:47

标签: mongodb aggregate

我有下面的集合(样本):

{
    "productResult": null,
    "suggestion": {
        "companyIndex6Months": null,
        "companyPageConfiguration": {}
    },
    "problemResult": null,
    "companyResult": "*",
    "complainResult": {
        "complains": {
            "prev": null,
            "next": null,
            "data": [{
                    "userState": "PR",
                    "userCity": "Curitiba",
                    "description": "TEST"
                }, {
                    "userState": "SP",
                    "userCity": "Guariba",
                    "description": "TEST 2"
                }
            ],
            "products": [{
                    "name": "Notebooks",
                    "id": "0000000000000370"
                }, {
                    "name": "Assistência técnica",
                    "id": "0000000000000470"
                }
            ],
            "problems": [{
                    "name": "Não liga",
                    "id": "0000000000000029"
                }, {
                    "name": "Lento",
                    "id": "0000000000000731",
                }
            ],
            "count": 30543
        },
        "suggestions": null,
        "maxScore": null
    }
},
{
    "productResult": null,
    "suggestion": {
        "companyIndex6Months": null,
        "companyPageConfiguration": {}
    },
    "problemResult": null,
    "companyResult": "*",
    "complainResult": {
        "complains": {
            "prev": null,
            "next": null,
            "data": [{
                    "userState": "RS",
                    "userCity": "Porto Alegre",
                    "description": "TEST"
                }, {
                    "userState": "SP",
                    "userCity": "Sao Paulo",
                    "description": "TEST 2"
                }
            ],
            "products": [{
                    "name": "Notebooks",
                    "id": "0000000000000370"
                }, {
                    "name": "Assistência técnica",
                    "id": "0000000000000470"
                }
            ],
            "problems": [{
                    "name": "Não liga 2",
                    "id": "0000000000000030"
                }, {
                    "name": "Lento 2",
                    "id": "0000000000000531",
                }
            ],
            "count": 30543
        },
        "suggestions": null,
        "maxScore": null
    }
}

我正在尝试返回此集合中所有文档的每个州的总发生次数(字段complainResult.data.userState)。

我对Mongo很新,我正在尝试使用下面的句子:

db.reclame.aggregate( [
   { $group: { _id, { state: "$complainResult.complains.data.userState" } , "count": { $sum: 1 } } }
]);

但它正在回归:

{ "_id" : { "state" : [ "MG", "PR", "SC", "SP", "MG", "RJ", "MG", "MG", "DF", "SP" ] }, "count" : 1 }
{ "_id" : { "state" : [ "SP", "SC", "MT", "SP", "SP", "PA", "BA", "SP", "RS", "SP" ] }, "count" : 1 }
{ "_id" : { "state" : [ "ES", "SP", "SP", "SP", "RS", "SP", "PE", "DF", "MG", "RJ" ] }, "count" : 1 }
{ "_id" : { "state" : [ "SP", "SP", "PR", "GO", "PR", "SP", "MG", "MG", "RJ", "BA" ] }, "count" : 1 }
{ "_id" : { "state" : [ "SP", "SP", "PR", "SP", "MG", "SP", "SP", "SP", "SP", "RJ" ] }, "count" : 1 }
{ "_id" : { "state" : [ "PE", "SP", "RS", "SP", "RJ", "BA", "SP", "RS", "SP", "CE" ] }, "count" : 1 }
{ "_id" : { "state" : [ "BA", "PR", "PE", "RJ", "SP", "SP", "SP", "SP", "PR", "MG" ] }, "count" : 1 }
{ "_id" : { "state" : [ "SP", "RN", "SP", "SP", "GO", "CE", "MT", "SC", "DF", "SP" ] }, "count" : 1 }
{ "_id" : { "state" : [ "SP", "RJ", "ES", "BA", "SP", "BA", "RS", "SP", "RS", "SP" ] }, "count" : 1 }
{ "_id" : { "state" : [ "CE", "SP", "SP", "MT", "RJ", "SC", "ES", "SP", "SP", "DF" ] }, "count" : 1 }
{ "_id" : { "state" : [ "SP", "PB", "SP", "SP", "MG", "AL", "SP", "MG", "SP", "SP" ] }, "count" : 1 }
{ "_id" : { "state" : [ "SP", "SP", "SP", "SP", "SP", "RJ", "RN", "CE", "RJ", "SP" ] }, "count" : 1 }
{ "_id" : { "state" : [ "SP", "PR", "DF", "SP", "SP", "SP", "DF", "MG", "RS", "SP" ] }, "count" : 1 }
{ "_id" : { "state" : [ "SP", "SP", "RS", "SP", "PR", "SP", "GO", "SP", "BA", "MG" ] }, "count" : 1 }
{ "_id" : { "state" : [ "GO", "RS", "PA", "ES", "SP", "SP", "SP", "BA", "SP", "PE" ] }, "count" : 1 }
{ "_id" : { "state" : [ "RJ", "SP", "RJ", "GO", "SP", "RJ", "RJ", "SP", "RO", "SP" ] }, "count" : 1 }
{ "_id" : { "state" : [ "SP", "SP", "SC", "RS", "SP", "SP", "PR", "RJ", "RJ", "SP" ] }, "count" : 1 }
{ "_id" : { "state" : [ "SP", "BA", "SP", "SP", "CE", "SC", "PR", "SP", "SP", "MG" ] }, "count" : 1 }
{ "_id" : { "state" : [ "SP", "PE", "SP", "DF", "RS", "RS", "PR", "DF", "RS", "SP" ] }, "count" : 1 }
{ "_id" : { "state" : [ "DF", "SP", "SP", "MT", "SP", "SP", "SP", "PR", "DF", "ES" ] }, "count" : 1 }

我的目标是返回这样的内容:

{ "_id" : { "state" : [ "MG"] }, "count" : 12 }
{ "_id" : { "state" : [ "RS"] }, "count" : 43 }
{ "_id" : { "state" : [ "SP"] }, "count" : 60 }

1 个答案:

答案 0 :(得分:0)

您需要$unwind然后$group

db.reclame.aggregate( [
   {$unwind : "$complainResult.complains.data" },
   {$group: { _id : { state: "$complainResult.complains.data.userState" } , "count": { $sum: 1 } } }
]);