How to get percentage total of data with group by date in MongoDB

六眼飞鱼酱① 提交于 2021-02-11 05:57:55

问题


How to get percentage total of data with group by date in MongoDB ?

Link example : https://mongoplayground.net/p/aNND4EPQhcb

I have some collection structure like this

{ 
    "_id" : ObjectId("5ccbb96706d1d47a4b2ced4b"), 
    "date" : "2019-05-03T10:39:53.108Z",  
    "id" : 166,   
    "update_at" : "2019-05-03T10:45:36.208Z",  
    "type" : "image"
}
{ 
    "_id" : ObjectId("5ccbb96706d1d47a4b2ced4c"), 
    "date" : "2019-05-03T10:39:53.133Z",  
    "id" : 166,   
    "update_at" : "2019-05-03T10:45:36.208Z", 
    "type" : "image"
}
{ 
    "_id" : ObjectId("5ccbb96706d1d47a4b2ced4d"), 
    "date" : "2019-05-03T10:39:53.180Z", 
    "id" : 166,  
    "update_at" : "2019-05-03T10:45:36.208Z", 
    "type" : "image"
}
{ 
    "_id" : ObjectId("5ccbb96706d1d47a4b2ced4e"), 
    "date" : "2019-05-03T10:39:53.218Z",  
    "id" : 166,  
    "update_at" : "2019-05-03T10:45:36.208Z",  
    "type" : "image"
}

And I have query in mongodb to get data of collection, how to get percentage of total data. in bellow example query to get data :

db.name_collection.aggregate(
   [
        { "$match": {  
            "update_at": { "$gte": "2019-11-04T00:00:00.0Z", "$lt": "2019-11-06T00:00:00.0Z"},
             "id": { "$in": [166] } 
        } },  
        {
           "$group" : { 
               "_id": {
                       $substr: [ '$update_at', 0, 10 ] 
                },
               "count" : {
                   "$sum" : 1
               } 
           }
        },
        {
            "$project" : {
                "_id" : 0,
                "date" : "$_id",
                "count" : "$count" 
            }
        },
        {
            "$sort" : {
                "date" : 1
            }
        }
   ]
)

and this response :

{ 
    "date" : "2019-11-04", 
    "count" : 39
},
{ 
    "date" : "2019-11-05", 
    "count" : 135
}

how to get percentage data total from key count ? example response to this :

{ 
    "date" : "2019-11-04", 
    "count" : 39,
    "percentage" : "22%"
},
{ 
    "date" : "2019-11-05", 
    "count" : 135,
    "percentage" : "78%"
}

回答1:


You have to group by null to get total count and then use $map to calculate the percentage. $round will be a useful operator in such case. Finally you can $unwind and $replaceRoot to get back the same number of documents:

db.collection.aggregate([
    // previous aggregation steps
    {
        $group: {
            _id: null,
            total: { $sum: "$count" },
            docs: { $push: "$$ROOT" }
        }
    },
    {
        $project: {
            docs: {
                $map: {
                    input: "$docs",
                    in: {
                        date: "$$this.date",
                        count: "$$this.count",
                        percentage: { $concat: [ { $toString: { $round: { $multiply: [  { $divide: [ "$$this.count", "$total" ] }, 100 ] } } }, '%' ] }
                    }
                }
            }
        }
    },
    {
        $unwind: "$docs"
    },
    {
        $replaceRoot: { newRoot: "$docs" }
    }
])

Mongo Playground



来源:https://stackoverflow.com/questions/59978235/how-to-get-percentage-total-of-data-with-group-by-date-in-mongodb

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