MongoDB aggregation - how to get a percentage value of how many times an event occurred per day of week

丶灬走出姿态 提交于 2019-12-08 03:08:18

问题


Im a MongoDB noob so please dont judge me if my question is stupid:P Im trying to get some results from MongoDB to create a table that would show percentage statistics of how much do i play a certain game per day of week (all games together per day = 100%). This is my JSON import for the database:

[
{"title":"GTA","date":"2017-11-13"},
{"title":"GTA","date":"2017-11-13"},
{"title":"BattleField","date":"2017-11-13"},
{"title":"BattleField","date":"2017-11-13"},
{"title":"BattleField","date":"2017-11-14"}
]

Ive written an aggregation that grouped the results by days and counted the total amount of times a game has been played per each day…:

db.games.aggregate([
    { $project: { _id: 0, date : { $dayOfWeek: "$date" }, "title":1} },
    { $group: { _id: {title: "$title", date: "$date"}, total: {$sum: 1} } }, 
    { $group: { _id: "$_id.date", types: {$addToSet: {title:"$_id.title", total: "$total"} } } }
])

…and this is what i got from MongoDB now:

/* 1 */
{
    "_id" : 3, 
    "types" : [
        {
            "title" : "BattleField",
            "total" : 1.0
        }
    ]
},

/* 2 */
{
    "_id" : 2, 
    "types" : [
        {
            "title" : "GTA",
            "total" : 2.0
        },
        {
            "title" : "BattleField",
            "total" : 2.0
        }
    ]
}

what i need to get is a table that would look like this:

            Monday      Tuesday
GTA         50,00%      0%
BattleField 50,00%      100%

Could you please advise me how can i get such percentage results from Mongo?


回答1:


Your attempt was pretty close to a solution! The following should point you in the right direction:

aggregate([
    { $project: { "_id": 0, "date" : { $dayOfWeek: "$date" }, "title": 1 } }, // get the day of the week from the "date" field
    { $group: { "_id": { "title": "$title", "date": "$date" }, "total": { $sum: 1 } } }, // group by title and date to get the total per title and date
    { $group: { "_id": "$_id.date", "types": { $push: { "title": "$_id.title", total: "$total" } }, "grandTotal": { $sum: "$total" } } }, // group by date only to get the grand total
    { $unwind: "$types" }, // flatten grouped items
    { $project: { "_id": 0, "title": "$types.title", "percentage": { $divide: [ "$types.total", "$grandTotal" ] }, "day": { $arrayElemAt: [ [ "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" ], "$_id" ] } } }, // calculate percentage and beautify output for "day"
])

Results:

{
    "title" : "BattleField",
    "percentage" : 0.5,
    "day" : "Tue"
}
{
    "title" : "GTA",
    "percentage" : 0.5,
    "day" : "Tue"
}
{
    "title" : "BattleField",
    "percentage" : 1.0,
    "day" : "Wed"
}


来源:https://stackoverflow.com/questions/47332116/mongodb-aggregation-how-to-get-a-percentage-value-of-how-many-times-an-event-o

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