Docs 菜单
Docs 主页
/
MongoDB Manual
/ / /

$dateToString(聚合)

在此页面上

  • 定义
  • 格式描述符
  • 例子
$dateToString

根据用户指定的格式将日期对象转换为字符串。

$dateToString表达式采用以下运算符表达式语法:

可以使用 $dateToString 查找托管在以下环境中的部署:

  • MongoDB Atlas :用于在云中部署 MongoDB 的完全托管服务

$dateToString表达式采用以下操作符表达式语法:

{ $dateToString: {
date: <dateExpression>,
format: <formatString>,
timezone: <tzExpression>,
onNull: <expression>
} }
- The date to convert to string. ``<dateExpression>`` must be a
valid :ref:`expression <aggregation-expressions>` that
resolves to a :ref:`Date <document-bson-type-date>`, a
:ref:`Timestamp <document-bson-type-timestamp>`, or an
:ref:`ObjectID <document-bson-type-object-id>`.
* - Field
- Description
- Optional. The date format specification. ``<formatString>``
can be any string literal, containing 0 or more format
specifiers. For a list of specifiers available, see
:ref:`format-specifiers`.
If unspecified and the ``timezone`` is specified and set to a
non UTC timezone, then ``$dateToString`` uses
``"%Y-%m-%dT%H:%M:%S.%L"`` as the default format.
If unspecified and the ``timezone`` is unspecified or
explicitly specified as UTC, then ``$dateToString`` uses
``"%Y-%m-%dT%H:%M:%S.%LZ"`` as the default format.
The date to convert to string. ``<dateExpression>`` must be a
valid :ref:`expression <aggregation-expressions>` that
resolves to a :ref:`Date <document-bson-type-date>`, a
:ref:`Timestamp <document-bson-type-timestamp>`, or an
:ref:`ObjectID <document-bson-type-object-id>`.
* - ``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 :ref:`format-specifiers`.
If unspecified, :expression:`$dateToString` uses
``"%Y-%m-%dT%H:%M:%S.%LZ"`` as the default format.
If unspecified, ``$dateToString`` returns null if the
``date`` is null or missing.

提示

另请参阅:

<formatString> 中可使用以下格式说明符:

说明符
说明
可能的值
%b

缩写月份名称(3 个字母)

7.0 版本中的新增功能

jan-dec
%B

完整月份名称

7.0 版本中的新增功能

january-december
%d
月中的某一天(2 位数字,补零)
01-31
%G
ISO 8601 格式的年份
0000-9999
%H
小时(2 位数字,填充零,24 小时时钟)
00-23
%j
年月日(3 位数字,填充零)
001-366
%L
毫秒(3 位数字,填充零)
000-999
%m
月份(2 位数字,补零)
01-12
%M
分钟(2 位数字,填充零)
00-59
%S
第二(2 位数字,补零)
00-60
%u
一周中每天的编号(采用 ISO 8601 格式,1-星期一,7-星期日)
1-7
%U
年中的某一周(2 位数字,补零)
00-53
%V
ISO 8601 格式的年中的某一周
01-53
%w
周中的某一天(1-星期日,7-星期六)
1-7
%Y
年份(4 位数字,补零)
0000-9999
%z
与 UTC 的时区偏移。
+/-[hh][mm]
%Z
从 UTC 开始偏移的分钟数。例如,如果时区偏移 (+/-[hhmm]) 为 +0445,则分钟偏移为 +285
+/-mmm
%%
作为文本的百分比字符
%

使用以下文档创建 sales 集合:

{
"_id" : 1,
"item" : "abc",
"price" : 10,
"quantity" : 2,
"date" : ISODate("2014-01-01T08:15:39.736Z")
}

以下聚合使用$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" } },
abbreviated_month: { $dateToString: {format: "%b", date: "$date", timezone: "+04:30" } },
full_month: { $dateToString: { format: "%B", date: "$date", timezone: "+04:30" } }
}
}
]
)

操作返回以下结果:

{
"_id" : 1,
"yearMonthDayUTC" : "2014-01-01",
"timewithOffsetNY" : "03:15:39:736-0500",
"timewithOffset430" : "12:45:39:736+0430",
"minutesOffsetNY" : "-300",
"minutesOffset430" : "270",
"abbreviated_month": "Jan",
"full_month": "January"
}
← $dateToParts(聚合)