$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.

format

Optional.可选。The date format specification. <formatString> can be any string literal, containing 0 or more format specifiers. For a list of specifiers available, see Format Specifiers.

If unspecified, $dateToString uses "%Y-%m-%dT%H:%M:%S.%LZ" as the default format.

Changed in version 4.0:The format field is optional if featureCompatibilityVersion (fCV) is set to "4.0" or greater. For more information on fCV, see setFeatureCompatibilityVersion.

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. If no timezone is provided, the result is displayed in UTC.

FormatExamples
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"

New in version 3.6.版本3.6中的新功能。

onNull

Optional.可选。The value to return if the date is null or missing. 如果date为空或缺少,则返回的值。The arguments can be any valid expression.参数可以是任何有效的表达式

If unspecified, $dateToString returns null if the date is null or missing.如果未指定,$dateToString在日期为null或缺失时返回null

New in version 4.0:Requires featureCompatibilityVersion (fCV) set to "4.0" or greater. For more information on fCV, see setFeatureCompatibilityVersion.

See also参阅

$toString and $convert

Format Specifiers格式说明符

The following format specifiers are available for use in the <formatString>:以下格式说明符可在<formatString>中使用:

Specifiers说明符Description描述Possible Values可能的值
%d Day of Month (2 digits, zero padded)月日(两位数,零填充) 01-31
%G

Year in ISO 8601 formatISO 8601格式的年份

New in version 3.4.版本3.4中的新功能。

0000-9999
%H Hour (2 digits, zero padded, 24-hour clock)小时(2位数字,零填充,24小时时钟) 00-23
%j Day of year (3 digits, zero padded)一年中的某一天(3位数字,零填充) 001-366
%L Millisecond (3 digits, zero padded)毫秒(3位数,零填充) 000-999
%m Month (2 digits, zero padded)月份(两位数,零填充) 01-12
%M Minute (2 digits, zero padded)分钟(两位数,零填充) 00-59
%S Second (2 digits, zero padded)秒(2位,零填充) 00-60
%w Day of week (1-Sunday, 7-Saturday)一周中的某一天(1-周日,7-周六) 1-7
%u

Day of week number in ISO 8601 format (1-Monday, 7-Sunday)ISO 8601格式的周数(1-周一、7-周日)

New in version 3.4.版本3.4中的新功能。

1-7
%U Week of year (2 digits, zero padded)一年中的一周(两位数,零填充) 00-53
%V

Week of Year in ISO 8601 formatISO 8601格式的一年中的一周

New in version 3.4.版本3.4中的新功能。

01-53
%Y Year (4 digits, zero padded)年份(4位数字,零填充) 0000-9999
%z

The timezone offset from UTC.相较之UTC的时区偏移。

New in version 3.6.版本3.6中的新功能。

+/-[hh][mm]
%Z

The minutes offset from UTC as a number. 分钟与UTC的偏移量为一个数字。For example, if the timezone offset (+/-[hhmm]) was +0445, the minutes offset is +285.例如,如果时区偏移量(+/-[hhmm])为+0445,则分钟偏移量为+285

New in version 3.6.版本3.6中的新功能。

+/-mmm
%% Percent Character as a Literal百分比字符作为文字 %

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:以下聚合使用$dateToStringdate字段作为格式化字符串返回:

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"
}