显示存储在 mongoDB 中的日期的星期几

哈尼·本·戈兹

我想以以下格式显示过去 7 天的每一天购买量:

{
    "sunday":30,
    "monday":20,
    ...
}  

数据库上的一次购买如下所示:

{
    _id: 603fcbcc691d8a5ecc320059
    productId: "603fc917a569f565687e2626"
    clientId: "1"
    totalPrice: 50
    date: 2021-03-02T00:00:00.000+00:00        // a date object
}

Purchase.aggregate([
                    { "$match": { "date": {$gte: new Date((new Date().getTime() - (7 * 24 * 60 * 60 * 1000))) } } },
                    { "$group": { 
                        "_id": { "day": { $substrCP: [ "$date", 0, 10 ] } },
                        "count": { $sum: 1 }
                    }},
                    { "$sort" : { "_id.day": 1}},
                    
                ])

我运行这段代码,我得到:

[
    {
        "_id": {
            "day": "2021-02-28"
        },
        "count": 30
    },
    {
        "_id": {
            "day": "2021-03-01"
        },
        "count": 20
    }
]
迪克莎·潘迪特

这可能对您有所帮助,但我在 mongodb 中找不到任何现成的方法,它为我提供了价值 1 的星期天。这就是为什么要保留 switch 语句。

collection.aggregate([
    { "$match": { "date": { $gte: new Date((new Date().getTime() - (7 * 24 * 60 * 60 * 1000))) } } },
    {
        "$group": {
            "_id": { $dayOfWeek: "$date" },
            "count": { $sum: 1 }
        }
    },
    { "$sort": { "_id": 1 } },
    {
        $group: {
            _id: null,
            data: {
                $push: {
                    k: {
                        $switch: {
                            branches: [
                                {
                                    case: { $eq: ["$_id", 1] },
                                    then: "monday"
                                },
                                {
                                    case: { $eq: ["$_id", 2] },
                                    then: "tuesday"
                                },
                                {
                                    case: { $eq: ["$_id", 3] },
                                    then: "wednesday"
                                },
                                
{
                                    case: { $eq: ["$_id", 4] },
                                    then: "thursday"
                                },
                               
 {
                                    case: { $eq: ["$_id", 5] },
                                    then: "friday"
                                },
                                
{
                                    case: { $eq: ["$_id", 6] },
                                    then: "saturday"
                                },

                                {
                                    case: { $eq: ["$_id", 7] },
                                    then: "sunday"
                                }]
                        }
                    },
                    v: "$count"
                }
            }
        }
    },
    {
        $project: {
            data: { $arrayToObject: "$data" }
        }
    }
])

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章