Database Manual / Reference / Query Language / Expressions

$isoDayOfWeek (expression operator)(表达式运算符)

Definition定义

$isoDayOfWeek

Returns the weekday number in ISO 8601 format, ranging from 1 (for Monday) to 7 (for Sunday).以ISO 8601格式返回工作日编号,范围从1(表示星期一)到7(表示星期日)。

The $isoDayOfWeek expression has the following operator expression syntax:$isoDayOfWeek表达式具有以下运算符表达式语法

{ $isoDayOfWeek: <dateExpression> }

The argument can be:参数可以是:

  • An expression that resolves to a Date, a Timestamp, or an ObjectID.解析为DateTimestampObjectID表达式
  • A document with this format:具有此格式的文档:

    { date: <dateExpression>, timezone: <tzExpression> }
    Field字段Description描述
    dateThe date to which the operator is applied. 运算符的应用日期。<dateExpression> must be a valid expression that resolves to a Date, a Timestamp, or an ObjectID.必须是解析为DateTimestampObjectID的有效表达式
    timezone

    Optional. 可选。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. <tzExpression>必须是解析为奥尔森时区标识符或UTC偏移格式的字符串的有效表达式If no timezone is provided, the result is in UTC.如果没有提供timezone,则结果为UTC。

    Format格式Examples示例
    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"

Behavior行为

Example示例Result结果
{ $isoDayOfWeek: new Date("2016-01-01") }
5
{ $isoDayOfWeek: { date: new Date("Jan 7, 2003") } }
2
{ $isoDayOfWeek: {
date: new Date("August 14, 2011"),
timezone: "America/Chicago"
} }
7
{ $isoDayOfWeek: ISODate("1998-11-07T00:00:00Z") }
6
{ $isoDayOfWeek: {
date: ISODate("1998-11-07T00:00:00Z"),
timezone: "-0400"
} }
5
{ $isoDayOfWeek: "March 28, 1976" }
error
{ $isoDayOfWeek: Date("2016-01-01") }
error
{ $isoDayOfWeek: "2009-04-09" }
error

Note

$isoDayOfWeek cannot take a string as an argument.$isoDayOfWeek不能接受字符串作为参数。

Example示例

A collection called birthdays contains the following documents:名为birthdays的集合包含以下文档:

db.birthdays.insertMany( [
{ _id: 1, name: "Betty", birthday: ISODate("1993-09-21T00:00:00Z") },
{ _id: 2, name: "Veronica", birthday: ISODate("1981-11-07T00:00:00Z") }
] )

The following operation returns the weekday number for each birthday field.以下操作返回每个birthdays字段的工作日编号。

db.birthdays.aggregate( [
{
$project: {
_id: 0,
name: "$name",
dayOfWeek: { $isoDayOfWeek: "$birthday" }
}
}
] )

The operation returns the following results:该操作返回以下结果:

[
{ name: "Betty", dayOfWeek: 2 },
{ name: "Veronica", dayOfWeek: 6 }
]