db.command.aggregate.dateFromString

将一个日期/时间字符串转换为日期对象

语法如下:

db.command.aggregate.dateFromString({
    dateString: <dateStringExpression>,
    timezone: <tzExpression>
})

示例

const $ = db.command.aggregate
db
  .collection('dates')
  .aggregate()
  .project({
    _id: 0,
    date: $.dateFromString({
        dateString: "2019-05-14T09:38:51.686Z"
    })
  })
  .end()

输出如下:

{
    "date": ISODate("2019-05-14T09:38:51.686Z")
}