$dateToString (aggregation)
On this page本页内容
Definition定义
$dateToString
-
Converts a date object to a string according to a user-specified format.根据用户指定的格式将日期对象转换为字符串。The$dateToString
expression has the following operator expression syntax:$dateToString
表达式具有以下运算符表达式语法:{ $dateToString: {
date: <dateExpression>,
format: <formatString>,
timezone: <tzExpression>,
onNull: <expression>
} }The$dateToString
takes a document with the following fields:$dateToString
接受一个具有以下字段的文档:Field字段Description描述date
Changed in version 3.6.3.6版更改。
The date to convert to string.要转换为字符串的日期。<dateExpression>
must be a valid expression that resolves to a Date, a Timestamp, or an ObjectID.必须是解析为Date、Timestamp或ObjectID的有效表达式。format
Optional.可选的。The date format specification.日期格式规范。<formatString>
can be any string literal, containing 0 or more format specifiers.可以是任何字符串文字,包含0个或多个格式说明符。For a list of specifiers available, see Format Specifiers.有关可用说明符的列表,请参阅格式说明符。
If unspecified,如果未指定,$dateToString
uses"%Y-%m-%dT%H:%M:%S.%LZ"
as the default format.$dateToString
将使用"%Y-%m-%dT%H:%M:%S.%LZ"
作为默认格式。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必须是一个有效的表达式,该表达式解析为格式为Olson时区标识符or a UTC Offset
.
或UTC偏移
的字符串。
If no如果没有提供timezone
is provided, the result is displayed inUTC
.timezone
,结果将以UTC
显示。Format格式Examples示例Olson Timezone IdentifierOlson时区标识符"America/New_York"
"Europe/London"
"GMT"UTC OffsetUTC偏移+/-[hh]:[mm], e.g. "+04:45"
+/-[hh][mm], e.g. "-0530"
+/-[hh], e.g. "+03"onNull
Optional.可选的。The value to return if the如果date
is null or missing.date
为null
或缺少,则返回的值。The arguments can be any valid expression.参数可以是任何有效的表达式。
If unspecified,如果未指定,如果日期为$dateToString
returns null if thedate
is null or missing.null
或缺少,$dateToString
将返回null
。
Format Specifiers格式规范
The following format specifiers are available for use in the 以下格式说明符可用于<formatString>
:<formatString>
:
%b | jan -dec | |
%B | january -december | |
%d | 01 -31 | |
%G | 0000 -9999 | |
%H | 00 -23 | |
%j | 001 -366 | |
%L | 000 -999 | |
%m | 01 -12 | |
%M | 00 -59 | |
%S | 00 -60 | |
%u | 1 -7 | |
%U | 00 -53 | |
%V | 01 -53 | |
%w | 0 -6 | |
%Y | 0000 -9999 | |
%z | +/-[hh][mm] | |
%Z | +/-[hhmm] ) was +0445 , the minutes offset is +285 .+/-hhmm] )为+0445 ,则分钟偏移量为+285 。 | +/-mmm |
%% | % |
Example实例
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 以下聚合使用$dateToString
to return the date
field as formatted strings:$dateToString
以格式化字符串的形式返回date
字段:
db.sales.aggregate(
[
{
$project: {
yearMonthDayUTC: { $dateToString: { format: "%Y-%m-%d", date: "$date" } },
timewithOffsetNY: { $dateToString: { format: "%H:%M:%S:%L%z", date: "$date", timezone: "America/New_York"} },
timewithOffset430: { $dateToString: { format: "%H:%M:%S:%L%z", date: "$date", timezone: "+04:30" } },
minutesOffsetNY: { $dateToString: { format: "%Z", date: "$date", timezone: "America/New_York" } },
minutesOffset430: { $dateToString: { format: "%Z", date: "$date", timezone: "+04:30" } }
}
}
]
)
The operation returns the following result:该操作返回以下结果:
{
"_id" : 1,
"yearMonthDayUTC" : "2014-01-01",
"timewithOffsetNY" : "03:15:39:736-0500",
"timewithOffset430" : "12:45:39:736+0430",
"minutesOffsetNY" : "-300",
"minutesOffset430" : "270"
}