On this page本页内容
$week
Returns the week of the year for a date as a number between 0 and 53.以0到53之间的数字返回日期的一年中的星期。
Weeks begin on Sundays, and week 1 begins with the first Sunday of the year. 星期天开始,第一周从一年中的第一个星期天开始。Days preceding the first Sunday of the year are in week 0. 一年中第一个星期日之前的天数为第0周。This behavior is the same as the "此行为与%U
" operator to the strftime
standard library function.strftime
标准库函数的“%U
”运算符相同。
The $week
expression has the following operator expression syntax:$week
表达式具有以下运算符表达式语法:
{ $week: <dateExpression> }
The argument can be:参数可以是:
A document with this format:具有以下格式的文档:
{ date: <dateExpression>, timezone: <tzExpression> }
date | <dateExpression> must be a valid expression that resolves to a Date, a Timestamp, or an ObjectID.<dateExpression> 必须是解析为Date、Timestamp或ObjectID的有效表达式。
| ||||||
timezone |
|
{ $week: new Date("Jan 1, 2016") } | 0 |
{ $week: { date: new Date("2016-01-04") } } | 1 |
{ $week: { date: new Date("August 14, 2011"), timezone: "America/Chicago" } } | 33 |
{ $week: ISODate("1998-11-01T00:00:00Z") } | 44 |
{ $week: { date: ISODate("1998-11-01T00:00:00Z"), timezone: "-0500" } } | 43 |
{ $week: "March 28, 1976" } | error |
{ $week: Date("2016-01-01") } | error |
{ $week: "2009-04-09" } | error |
$week
不能将字符串作为参数。Consider a 考虑使用以下文档的sales
collection with the following document:sales
集合:
{ "_id" : 1, "item" : "abc", "price" : 10, "quantity" : 2, "date" : ISODate("2014-01-01T08:15:39.736Z") }
The following aggregation uses the 以下聚合使用$week
and other date operators to break down the date
field:$week
和其他日期运算符细分date
字段:
db.sales.aggregate( [ { $project: { year: { $year: "$date" }, month: { $month: "$date" }, day: { $dayOfMonth: "$date" }, hour: { $hour: "$date" }, minutes: { $minute: "$date" }, seconds: { $second: "$date" }, milliseconds: { $millisecond: "$date" }, dayOfYear: { $dayOfYear: "$date" }, dayOfWeek: { $dayOfWeek: "$date" }, week: { $week: "$date" } } } ] )
The operation returns the following result:该操作返回以下结果:
{ "_id" : 1, "year" : 2014, "month" : 1, "day" : 1, "hour" : 8, "minutes" : 15, "seconds" : 39, "milliseconds" : 736, "dayOfYear" : 1, "dayOfWeek" : 4, "week" : 0 }