如何使用MongoDB $ group阶段对重复值进行分组和计数?

时间:2016-10-25 17:59:08

标签: mongodb

我在聚合中遇到$ group阶段的问题。我想将所有“recentPlays.quiz”值组合在一起并计算重复值,因此我想从聚合中得到的最终结果是两个字段:测验对象和总计。在这种情况下,它将是:

{
        "recentPlays" : [
            {
                "quiz" : {
                    "author" : "red-tester1",
                    "title" : "Asdffff Dfasdf"
                },
                "count": 1
            },
            {
                "quiz" : {
                    "author" : "red-tester3",
                    "title" : "Creation Test 2"
                },
                "count": 1
            },
            {
                "quiz" : {
                    "author" : "blue-tester1",
                    "title" : "Finky Fink"
                },
                "count": 4
            }
        ]
}

这是我到目前为止的聚合:

db.users.aggregate([
    {$match: { "recentPlays.date": {$gte:twentyFourHrsAgo}}},
    {$project: {"recentPlays.quiz":1, _id:0}}
]).pretty();

这是聚合的输出:

MongoDB shell version: 3.2.1
connecting to: videoQuiz
{
    "recentPlays" : [
        {
            "quiz" : {
                "author" : "red-tester1",
                "title" : "Asdffff Dfasdf"
            }
        },
        {
            "quiz" : {
                "author" : "red-tester3",
                "title" : "Creation Test 2"
            }
        },
        {
            "quiz" : {
                "author" : "blue-tester1",
                "title" : "Finky Fink"
            }
        },
        {
            "quiz" : {
                "author" : "blue-tester1",
                "title" : "Finky Fink"
            }
        },
        {
            "quiz" : {
                "author" : "blue-tester1",
                "title" : "Finky Fink"
            }
        },
        {
            "quiz" : {
                "author" : "blue-tester1",
                "title" : "Finky Fink"
            }
        }
    ]
}

以下是整个系列:

MongoDB shell version: 3.2.1
connecting to: videoQuiz
{
    "_id" : ObjectId("580f7be62c6fd3c8065577f5"),
    "user" : "blue-tester1",
    "email" : "aslfjjcc@lkcjasdc.com",
    "createdAt" : ISODate("2016-10-25T15:36:06.933Z"),
    "recentPlays" : [
        {
            "quiz" : {
                "author" : "red-tester1",
                "title" : "Asdffff Dfasdf"
            },
            "score" : "0",
            "date" : ISODate("2016-10-25T15:36:27.546Z")
        },
        {
            "quiz" : {
                "author" : "red-tester3",
                "title" : "Creation Test 2"
            },
            "score" : "100",
            "date" : ISODate("2016-10-25T15:37:09.142Z")
        }
    ],
    "mostRecentQuiz" : {
        "author" : "red-tester3",
        "title" : "Creation Test 2"
    },
    "mostRecentQuizTime" : ISODate("2016-10-25T15:37:09.142Z"),
    "plays" : 2
}
{
    "_id" : ObjectId("580a5dea650296d808082e65"),
    "user" : "red-tester3",
    "email" : "aldkdk@ccc.com",
    "createdAt" : ISODate("2016-10-21T18:26:50.870Z"),
    "recentPlays" : [
        {
            "quiz" : {
                "author" : "red-tester2",
                "title" : "TOP PLAYED QUIZ - Today"
            },
            "score" : "0",
            "date" : ISODate("2016-10-21T18:27:16.292Z")
        },
        {
            "quiz" : {
                "author" : "red-tester2",
                "title" : "TOP LIKED QUIZ - TODAY"
            },
            "score" : "100",
            "date" : ISODate("2016-10-21T18:27:32.788Z")
        },
        {
            "quiz" : {
                "author" : "red-tester2",
                "title" : "TOP LIKED QUIZ - TODAY"
            },
            "score" : "100",
            "date" : ISODate("2016-10-21T18:27:44.497Z")
        },
        {
            "quiz" : {
                "author" : "Bertram",
                "title" : "frfrf"
            },
            "score" : "100",
            "date" : ISODate("2016-10-21T18:28:43.893Z")
        },
        {
            "quiz" : {
                "author" : "Bertram",
                "title" : "Here We Go With the New Thing"
            },
            "score" : "0",
            "date" : ISODate("2016-10-21T18:43:51.205Z")
        },
        {
            "quiz" : {
                "author" : "red-tester3",
                "title" : "Presidents of the United States"
            },
            "score" : "0",
            "date" : ISODate("2016-10-23T00:53:29.167Z")
        },
        {
            "quiz" : {
                "author" : "red-tester3",
                "title" : "Presidents of the United States"
            },
            "score" : "0",
            "date" : ISODate("2016-10-23T00:53:44.815Z")
        },
        {
            "quiz" : {
                "author" : "red-tester3",
                "title" : "Creation Test 1"
            },
            "score" : "100",
            "date" : ISODate("2016-10-23T23:50:55.355Z")
        },
        {
            "quiz" : {
                "author" : "red-tester3",
                "title" : "Creation Test 2"
            },
            "score" : "100",
            "date" : ISODate("2016-10-23T23:52:33.210Z")
        },
        {
            "quiz" : {
                "author" : "red-tester3",
                "title" : "Here Is a New Title"
            },
            "score" : "100",
            "date" : ISODate("2016-10-23T23:58:53.683Z")
        }
    ],
    "mostRecentQuiz" : {
        "author" : "red-tester3",
        "title" : "Here Is a New Title"
    },
    "mostRecentQuizTime" : ISODate("2016-10-23T23:58:53.683Z"),
    "plays" : 10,
    "likedQuizzes" : [
        {
            "title" : "TOP LIKED QUIZ - TODAY",
            "author" : "red-tester2",
            "date" : ISODate("2016-10-21T18:27:34.893Z")
        },
        {
            "title" : "frfrf",
            "author" : "Bertram",
            "date" : ISODate("2016-10-21T18:28:45.863Z")
        },
        {
            "title" : "Here We Go With the New Thing",
            "author" : "Bertram",
            "date" : ISODate("2016-10-21T18:43:53.148Z")
        }
    ],
    "createdQuizzes" : [
        {
            "title" : "Yeah Here We Go",
            "id" : ObjectId("580a63f274b9a89c061f973e")
        },
        {
            "title" : "Z Alpha",
            "id" : ObjectId("580a641474b9a89c061f973f")
        },
        {
            "title" : "Tags Limit Test",
            "id" : ObjectId("580a6bda8d8049ac0bc1df2e")
        },
        {
            "title" : "Tags Limit test2",
            "id" : ObjectId("580a6bf98d8049ac0bc1df2f")
        },
        {
            "title" : "Presidents of the United States",
            "id" : ObjectId("580c09d28d8049ac0bc1df30")
        },
        {
            "title" : "Creation Test 1",
            "id" : ObjectId("580d4cca8d8049ac0bc1df31")
        },
        {
            "title" : "Creation Test 2",
            "id" : ObjectId("580d4d2d8d8049ac0bc1df32")
        },
        {
            "title" : "Here Is a New Title",
            "id" : ObjectId("580d4ead8d8049ac0bc1df33")
        }
    ]
}

提前感谢任何指导。请原谅这些文件中的虚拟文本,仅供测试之用。

1 个答案:

答案 0 :(得分:1)

这将是一个两步过程。第一步是$unwind" recentPlays"阵列。第二步是$group by" recentPlays.quiz"。

例如:

db.users.aggregate([
    { "$match" : { "recentPlays.date": { "$gte" : twentyFourHrsAgo}}},
    { "$project" : {"recentPlays.quiz":1, _id:0}},
    { "$unwind" : "$recentPlays" },
    { "$group" : { "_id" : "$recentPlays.quiz", "total" : { "$sum" : 1 } } }
]).pretty();