mongoDB取日、周、月、年、时段、分钟、秒

 

  1.$dateFromString字符串转时间类型
  2.$dateToString 日期转字符串
  3.周 $isoWeek
  4.周的第XX天 $isoDayOfWeek、$dayOfWeek
  5.月 $month
  6.年 $year
  7.取时段 $hour
  8.取分钟 $minute
  9.取秒 $second

 

1.$dateFromString字符串转时间类型

{

  $dateFromString:{

  dateString:<dateStringExpression>,   //要转化的时间字符串

  format:<formatExpression>,   //转换的格式,’%Y-%m-%dT%H:%M:%S.%LZ‘    

  timezone:<tzExpression>,   //指定时区

  onError:<onErrorExpression>,   //报错时输出

  onNull:<onNullExpression>   //null时输出

  }

}

如1:
{$dateFromString:{
        dateString:'2017-02-16T12:16:28.787'
    }
}
结果:ISODate('2017-02-16T12:16:28.787Z')

如2:
{$dateFromString:{
        dateString:'15-06-2018',
        fromat:'%d-%m-%Y'
    }
}
结果:ISODate('2018-06-15T00:00:0.000Z')

 

2.$dateToString 日期转字符串

{$dateToString:{

  dateToString:<dateExpression>,   

  format:<formatExpression>,    

  timezone:<tzExpression>,   

  onNull:<onNullExpression>   

}

 

3.周 $isoWeek

语法:
{ $isoWeek: { date: <dateExpression>, timezone: <tzExpression> } }
① data_time是日期类型
直接使用$isoWeek取周

isoWeek: {
  $isoWeek: {
  "$data_time",
  "timezone": "Asia/Shanghai"
  }
}

① data_time是字符串类型
要先用$dateFromString将字符串转成日期类型,再用$isoWeek取周

isoWeek: {
  $isoWeek: {
  date: {
  "$dateFromString": {
    "dateString": "$data_time"
    }
  },
  "timezone": "Asia/Shanghai"
  }
}

 

 

4.周的第XX天 $isoDayOfWeek、$dayOfWeek

MongoDB中保存的datatime类型的数据是世界时间,需要转成中国的东八区的时间。世界时间如:ISODate("2023-11-22T00:00:00.000Z")

 

$isoDayOfWeek、$dayOfWeek都是取周的第XX天,区别:$isoDayOfWeek周一是周的第一天,$dayOfWeek周日是周的第一天
语法:
{ $isoDayOfWeek: { date: <dateExpression>, timezone: <tzExpression> } }
{ $dayOfWeek: { date: <dateExpression>, timezone: <tzExpression> } }

 

>>>$isoDayOfWeek:周一是周的第一天
如data_time是字符串类型

isoDayOfWeek: {
    $isoDayOfWeek: {
        date: {
            "$dateFromString": {
                "dateString": "$data_time"
            }
        },
        "timezone": "Asia/Shanghai"
    }
}

 

>>>$dayOfWeek:周日是周的第一天
如data_time是字符串类型

dayOfWeek: {
    $dayOfWeek: {
        date: {
            "$dateFromString": {
                "dateString": "$data_time"
            }
        },
        "timezone": "Asia/Shanghai"
    }
}

如想使用$dayOfWeek,且最终想得到的结果是周一是周的第一天,则可借助$cond条件判断,如下:
dayOfWeek:{$dayOfWeek:{date: {"$dateFromString": {"dateString": "$data_time"}}, "timezone": "Asia/Shanghai"}} 是取周的第几天,返回结果周日是周的第一天,页面上展示周一、周二样式,则需是周一是周的第一天,减1后周一则是1,但有个问题周日就为0了,则使用$cond判断,如果dayOfWeek返回的值-1大于0,则按原值返回,如为0,则置成7,即周日
步骤一: dayOfWeek:{$dayOfWeek:{date: {"$dateFromString": {"dateString": "$data_time"}}, "timezone": "Asia/Shanghai"}}
步骤二: weekDay:{$cond:{if:{'$gt':[{$subtract:['$dayOfWeek',1]},0]},then:{$subtract:['$dayOfWeek',1]},else:7}}

例1:

db.collection_2023.aggregate(
    {$set:{_id:'2023'}},
    {$unionWith:{coll:'collection_2022',pipeline:[ { $set: { _id: "2022" } } ]}},
    {$unionWith:{coll:'collection_2024',pipeline:[ { $set: { _id: "2024" } } ]}},
    {$match:{rid:'P00001',data_time:{$gte:'2024-01-01',$lte:'2024-01-31'}}},
    {$project:{data_time:'$data_time',isoWeek:{$isoWeek:{date: {"$dateFromString": {"dateString": "$data_time"}}, "timezone": "Asia/Shanghai"}},
    dayOfWeek:{$dayOfWeek:{date: {"$dateFromString": {"dateString": "$data_time"}}, "timezone": "Asia/Shanghai"}},innum:1}},
    {$project:{data_time:1,isoWeek:1,dayOfWeek:1,weekDay:{$cond:{if:{'$gt':[{$subtract:['$dayOfWeek',1]},0]},then:{$subtract:['$dayOfWeek',1]},else:7}},innum:1}}
)

 例2:

db.collection_2023.aggregate(
    {$set:{_id:'2023'}},
    {$unionWith:{coll:'collection_2022',pipeline:[ { $set: { _id: "2022" } } ]}},
    {$unionWith:{coll:'collection_2024',pipeline:[ { $set: { _id: "2024" } } ]}},
    {$match:{rid:{$in:['P00001']},data_time:{$gte:'2022-10-01',$lte:'2024-03-07'}}},
    {$project:{rid:1,data_time:1,isoWeek:{$isoWeek:{date: {"$dateFromString": {"dateString": "$data_time"}}, "timezone": "Asia/Shanghai"}},
    isoDayOfWeek:{$isoDayOfWeek:{date: {"$dateFromString": {"dateString": "$data_time"}}, "timezone": "Asia/Shanghai"}},
    dayOfWeek:{$dayOfWeek:{date: {"$dateFromString": {"dateString": "$data_time"}}, "timezone": "Asia/Shanghai"}},
    innum:1}}
    
)

 

5.月 $month

>>>日期类型取年:$month

语法:{ $month: <dateExpression> }

 >>>字符串类型取月:字符串截取$substr

举例见文末

 

6.年 $year

>>>日期类型取年:$year

语法:{ $year: <dateExpression> }

 >>>字符串类型取年:字符串截取$substr

 举例见文末

 

7.取时段 $hour

 

>>>日期类型取小时:$hour

语法:{ $hour: <dateExpression> }

 >>>字符串类型取时段(30min和60min):字符串截取$substr

通过字符串截取实现

如datatime类型取时段,可先将datatime类型转成字符串,再通过字符串截取取时段

$substr: [{
    $dateToString: {
        format: '%Y-%m-%d %H:%M:%S',
        date: '$num_data.dt',
        timezone: 'Asia/Shanghai'
    }
}, 11, 5]

结果: 如2023-07-08T01:00:00.000Z  取时段为09:00

$substr: [{
    $dateToString: {
        format: '%Y-%m-%d %H:%M:%S',
        date: '$num_data.dt',
        timezone: 'Asia/Shanghai'
    }
}, 11, 2]

结果:如2023-07-08T01:00:00.000Z  取时段为09

 

8.取分钟 $minute

>>>日期类型取年:$minute

语法:{ $minute: <dateExpression> }

 >>>字符串类型取分钟:字符串截取$substr

 

9.取秒 $second

>>>日期类型取年:$second

语法:{ $second: <dateExpression> }

 >>>字符串类型取秒:字符串截取$substr

 

例1:

日期类型字段取年、月、小时、分钟、秒

db.traffic_site_day_2023.aggregate(
{$project:{_id:'ISODate("1998-11-02T00:00:00Z")',isoWeek:{ $isoWeek: ISODate("1998-11-02T15:42:16Z") },
  year:{ $year: ISODate("1998-11-02T15:42:16Z") },
  month:{ $month: ISODate("1998-11-02T15:42:16Z") },
  hour:{ $hour: ISODate("1998-11-02T15:42:16Z") },
  minute:{ $minute: ISODate("1998-11-02T15:42:16Z") },
  second:{ $second: ISODate("1998-11-02T15:42:16Z") }
}},
{$limit:1}
    )

例2:

字符串类型字段取年、月、小时、分钟、秒

db.traffic_site_day_2023.aggregate(
{$project:{_id:'1998-11-02 15:42:16',
  year:{$substr:['1998-11-02 15:42:16',0,4]},
  month:{$substr:['1998-11-02 15:42:16',5,2]},
  hour:{$substr:['1998-11-02 15:42:16',11,2]},
  minute:{$substr:['1998-11-02 15:42:16',14,2]},
  second:{$substr:['1998-11-02 15:42:16',17,2]}
}},
{$limit:1}
    )

 

posted @ 2023-12-22 10:51  紫蕈  阅读(654)  评论(0编辑  收藏  举报