【问题标题】:How to get percentage total of data with group by date in MongoDB如何在MongoDB中获取按日期分组的数据百分比
【发布时间】:2020-01-30 02:19:17
【问题描述】:

如何在 MongoDB 中获取按日期分组的数据百分比?

链接示例:https://mongoplayground.net/p/aNND4EPQhcb

我有一些这样的集合结构

{ 
    "_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"
}

我在 mongodb 中有查询以获取集合数据,如何获取总数据的百分比。在下面的示例查询中获取数据:

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
            }
        }
   ]
)

这个回应:

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

如何从键计数中获取百分比数据总数?对此的示例响应:

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

【问题讨论】:

    标签: mongodb mongodb-query


    【解决方案1】:

    您必须按null 分组以获取total 计数,然后使用$map 计算百分比。在这种情况下,$round 将是一个有用的运算符。最后你可以$unwind$replaceRoot取回相同数量的文档:

    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

    【讨论】:

    • 我想我的意思是在“按日期分组然后添加总百分比”之后找到一种方法来说明按日期分组后的方式。可以再次查看我的样本集吗?
    • @MiftachHidayatullah 您的聚合完全没问题,您只需要在现有代码中添加更多阶段,这就是为什么我留下此评论(// 之前的聚合步骤)以表明我的阶段是您的延续管道
    • @MiftachHidayatullah 考虑你的例子:mongoplayground.net/p/KEB4d58q0OD
    • 我试过了,但是显示错误“mongodb unrecognized expression '$tostring'”,这是mongodb版本问题吗?
    • 是的,您需要升级到4.0 或更高版本
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 2022-12-12
    • 2017-10-22
    • 1970-01-01
    • 2013-10-30
    相关资源
    最近更新 更多