定义
$isoWeek以 ISO 8601 格式返回周数,范围从
1到53。周数从1开始,即包含一年中第一个星期四的那个星期(星期一到星期日)。The
$isoWeekexpression has the following operator expression syntax:{ $isoWeek: <dateExpression> } 参数可以是:
具有此格式的文档:
{ date: <dateExpression>, timezone: <tzExpression> } 字段说明datetimezoneOptional. The timezone of the operation result.
<tzExpression>must be a valid expression that resolves to a string formatted as either an Olson Timezone Identifier or a UTC Offset. If notimezoneis provided, the result is in UTC.format示例Olson Timezone Identifier"America/New_York" "Europe/London" "GMT" UTC Offset+/-[hh]:[mm], e.g. "+04:45" +/-[hh][mm], e.g. "-0530" +/-[hh], e.g. "+03"
行为
例子 | 结果 | ||||
|---|---|---|---|---|---|
| 1 | ||||
| 53 | ||||
| 32 | ||||
| 45 | ||||
| 44 | ||||
|
| ||||
|
| ||||
|
|
注意
$isoWeek 不能将字符串作为参数。
例子
名为 deliveries 的集合包含以下文档:
db.deliveries.insertMany( [ { _id: 1, date: ISODate("2006-10-24T00:00:00Z"), city: "Boston" }, { _id: 2, date: ISODate("2011-08-18T00:00:00Z"), city: "Detroit" } ] )
以下操作返回每个date字段的周数。
db.deliveries.aggregate( [ { $project: { _id: 0, city: "$city", weekNumber: { $isoWeek: "$date" } } } ] )
操作返回以下结果:
[ { city: "Boston", weekNumber: 43 }, { city: "Detroit", weekNumber: 33 } ]