MongoDB-$ week函数和聚合的第一天 [英] MongoDB - $week function and first day of week in aggregation

查看:212
本文介绍了MongoDB-$ week函数和聚合的第一天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用$ project运算符从Date中提取星期部分,然后在汇总管道中按星期分组.

I use $project operator to extract week part from Date, then do grouping by weeks in aggregation pipeline.

{ $project: { 
    year: { $year: [ "$datetime" ] }, 
    week: { $week: [ "$datetime" ] }, 
    ....
    }
},
....
{ $group: { 
    _id: { 
        year: "$year", 
        week: "$week", 
        },
        .....
    }
}

但是,我为此使用的$ week运算符始终将星期日视为一周中的第一天,而在我们的位置中,我们将星期一用作一周中的第一天.因此,按周分组的结果对我而言总是不正确的. 有什么方法可以解决这个问题或已知的解决方法?

But $week operator, which I use for this, always counts Sunday as a first day of week, and we use Monday as first day of week in our location. Therefore, the results grouped by week are always incorrect for me. Is there any way how to deal with this, or known workaround?

mongo跟踪系统中的现有请求似乎已经解决了一年多了(真的是很少需要的选项吗?) 欢迎任何可能的选择.也许有可能在javascript中创建一些自定义函数,然后在某处添加/替换它?

The existing request in mongo tracking system seems to be unresolved for more than a year (is it really so rarely needed option?) Any possible options are welcome. Maybe there is possibility to create some custom function in javascript and add/replace it somewhere?

推荐答案

MongoDB最终在3.4中添加了$isoWeek$isoWeekYear,这将从星期一开始.

MongoDB has finally added $isoWeek and $isoWeekYear in 3.4 which will start the week on a Monday.

更多信息: https://docs.mongodb.com/manual/参考/操作员/聚合/isoWeek/

这篇关于MongoDB-$ week函数和聚合的第一天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆