MongoDB聚合运算符:$toDate

本文介绍了MongoDB中的$toDate聚合运算符,用于将不同类型的值转换为日期格式,包括Double、Decimal、Long、String和ObjectId。通过实例展示了如何在聚合管道中使用该运算符进行日期转换并按日期排序文档。
摘要由CSDN通过智能技术生成

MongoDB聚合运算符:$toDate


$toDate聚合运算符将指定的值转换为日期类型,如果无法转换则返回错误,如果指定的值为 null或引用缺失字段,返回 null

语法

{
   $toDate: <expression>
}

$toDate接受任何有效的表达式。

$toDate$convert表达式的简写形式:

{ $convert: { input: <expression>, to: "date" } }

使用

下表列出了可转换为日期值的类型:

输入类型规则
Double返回与截断双精度值表示的毫秒数相对应的日期,其中正数对应1970年1月1日以来的毫秒数,负数对应1970年1月1日之前的毫秒数。
Decimal返回与截断双精度值表示的毫秒数相对应的日期,其中正数对应1970年1月1日以来的毫秒数,负数对应1970年1月1日之前的毫秒数。
Long返回与long值所代表的毫秒数相对应的日期,其中正数对应1970年1月1日以来的毫秒数,负数对应1970年1月1日之前的毫秒数。
String返回与日期字符串对应的日期,字符串必须是有效的日期,如:"2018-03-20""2018-03-20T12:00:00Z""2018-03-20T12:00:00+0500"
ObjectId返回与 ObjectId 的时间戳对应的日期
Timestamp返回与时间戳对应的日期

下表列出了一些转换为日期值的示例:

示例结果
{$toDate: 120000000000.5}ISODate("1973-10-20T21:20:00Z")
{$toDate: NumberDecimal("1253372036000.50")}ISODate("2009-09-19T14:53:56Z")
{$toDate: NumberLong("1100000000000")}ISODate("2004-11-19T11:33:20Z")
{$toDate: NumberLong("-1100000000000")}ISODate("1935-02-22T12:26:40Z")
{$toDate: ObjectId("5ab9c3da31c2ab715d421285")}ISODate("2018-03-27T04:08:58Z")
{$toDate: "2018-03-20"}ISODate("2018-03-20T00:00:00Z")
{$toDate: "2018-03-20 11:00:06 +0500"}ISODate("2018-03-20T06:00:06Z")
{$toDate: "Friday"}Error
{$toDate: Timestamp({ t: 1637688118, i: 1 })}ISODate("2021-11-23T17:21:58.00Z")

举例

使用下面的脚本创建orders集合:

db.orders.insertMany( [
   { _id: 1, item: "apple", qty: 5, price: 2, order_date: new Date( "2018-03-20" ) },
   { _id: 2, item: "pie", qty: 10, price: 3, order_date: new Date( "2018-03-22" ) },
   { _id: 3, item: "ice cream", qty: 2, price: 4, order_date: "2018-03-15" },
   { _id: 4, item: "almonds" , qty: 5, price: 7, order_date: "2018-03-15 +10:00" }
] )

下面的聚合对订单集合orders进行操作,在按日期值排序之前将order_date转换为日期:

//定义转换日期类型的阶段

dateConversionStage = {
   $addFields: {
      convertedDate: { $toDate: "$order_date" }
   }
};

//定义排序的阶段

sortStage = {
   $sort: { "convertedDate": 1 }
};

db.orders.aggregate( [
   dateConversionStage,
   sortStage
] )

执行的结果为:

{
   _id: 4,
   item: 'almonds',
   qty: 5,
   price: 7,
   order_date: '2018-03-15 +10:00',
   convertedDate: ISODate("2018-03-14T14:00:00.000Z")
},
{
   _id: 3,
   item: 'ice cream',
   qty: 2,
   price: 4,
   order_date: '2018-03-15',
   convertedDate: ISODate("2018-03-15T00:00:00.000Z")
},
{
   _id: 1,
   item: 'apple',
   qty: 5,
   price: 2,
   order_date: ISODate("2018-03-20T00:00:00.000Z"),
   convertedDate: ISODate("2018-03-20T00:00:00.000Z")
},
{
   _id: 2,
   item: 'pie',
   qty: 10,
   price: 3,
   order_date: ISODate("2018-03-22T00:00:00.000Z"),
   convertedDate: ISODate("2018-03-22T00:00:00.000Z")
}
  • 10
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

原子星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值