MongoDB 您所在的位置:网站首页 mongodb日期格式去掉毫秒 MongoDB

MongoDB

2024-07-07 01:54| 来源: 网络整理| 查看: 265

语法:

{      $dateFromString: {         dateString: ,         format: ,         timezone: ,         onError: ,         onNull:     }  }  语法说明:

(1)dateString: :代表的是需要转换成日期的字符串表达式

(2)format: :可选,代表的是转换格式表达式,默认使用:"%Y-%m-%dT%H:%M:%S.%LZ"

(3)timezone: :可选,代表的是转换成日期时使用的时区的表达式

(4)onError: :可选,代表的是转换失败时的处理方式的表达式

(5)onNull: :可选,代表的是Null的情况下的处理方式的表达式

从 timestamp 转换为日期取决于我们保存时间戳的类型。它是对象、数字还是字符串类型。

我们可以在 mongo shell 上使用以下命令检查字段的类型。在本教程中,我们将学习如何将时间戳转换为数字、字符串或对象类型的日期。

检查字段类型:

// MongoDB 5.0.8 > typeof db.collection_name.findOne().fieldName; 一、当时间戳为数字类型时,将时间戳转换为日期

示例代码(用于 collection1):

// MongoDB 5.0.8 > db.collection1.insertMany([ {"_id": 1, "datetime": new Date().getTime()}, //saves timestamp in milliseconds {"_id": 2, "datetime": new Date().getTime()}, {"_id": 3, "datetime": new Date().getTime()}, {"_id": 4, "datetime": new Date().getTime()}, {"_id": 5, "datetime": new Date().getTime()} ]); > db.collection1.find();

 输出:

{ "_id" : 1, "datetime" : 1655448286502 } { "_id" : 2, "datetime" : 1655448286502 } { "_id" : 3, "datetime" : 1655448286502 } { "_id" : 4, "datetime" : 1655448286502 } { "_id" : 5, "datetime" : 1655448286502 }

 检查 datetime 字段的类型:

// MongoDB 5.0.8 > typeof db.collection1.findOne().datetime;

输出:

number

一旦集合准备好并且我们知道字段类型,我们可以使用以下方法将时间戳转换为日期并计算每个日期的条目。

示例代码(用于 collection1):

// MongoDB 5.0.8 > db.collection1.aggregate([ { "$project": { "_id": { "$toDate": "$datetime" } } }, { "$group": { "_id": { "$dateToString": { "format": "%Y-%m-%d", "date": "$_id" }}, "count": { "$sum": 1 } } } ]);

输出:

{ "_id" : "2022-06-17", "count" : 5 }

在这里,我们使用 $project 聚合阶段,它从指定集合中获取文档并告知字段的包含、_id 字段的抑制、新字段的添加以及重置现有字段的值。

在 $project 阶段,我们使用 $toDate 聚合将 datetime 字段的值转换为日期,并将其保存在 _id 字段中,该字段进一步传递给 $group 聚合阶段。

在这个阶段,我们使用 $dateToString 聚合管道运算符将指定的 date 对象按照指定的格式转换为字符串,并保存在 _id 字段中,进一步用于对文档进行分组。

$dateToString 采用 timestamp、date 或 ObjectId,根据用户指定的格式进行进一步转换,而 $sum 仅返回数值的总和。

最后,我们按项目对文档进行分组,这里是 _id。请记住,_id 现在包含一个字符串值,因为我们根据用户指定的格式将指定的日期转换为字符串。

二、当时间戳为字符串类型时,将时间戳转换为日期

示例代码(用于 collection2):

// MongoDB 5.0.8 > db.collection2.insertMany([ {"_id": 1, "datetime": "1655445247168"}, {"_id": 2, "datetime": "1522838153324"}, {"_id": 3, "datetime": "1513421466415"}, {"_id": 4, "datetime": "1515488183153"}, {"_id": 5, "datetime": "1521571234500"} ]); > db.collection2.find();

输出:

{ "_id" : 1, "datetime" : "1655445247168" } { "_id" : 2, "datetime" : "1522838153324" } { "_id" : 3, "datetime" : "1513421466415" } { "_id" : 4, "datetime" : "1515488183153" } { "_id" : 5, "datetime" : "1521571234500" }

检查 datetime 字段的类型:

// MongoDB 5.0.8 > typeof db.collection2.findOne().datetime;

输出:

string

在这个集合中,我们有字符串格式的时间戳。因此,我们可以使用以下解决方案将其从时间戳转换为日期,并按日期对它们进行分组。

示例代码(用于 collection2):

// MongoDB 5.0.8 > db.collection2.aggregate([ { "$project": { "_id": { "$toDate": { "$toLong": "$datetime" }} } }, { "$group": { "_id": { "$dateToString": { "format": "%Y-%m-%d", "date": "$_id" } }, "count": { "$sum": 1 } } } ]);

输出:

{ "_id" : "2018-03-20", "count" : 1 } { "_id" : "2017-12-16", "count" : 1 } { "_id" : "2022-06-17", "count" : 1 } { "_id" : "2018-04-04", "count" : 1 } { "_id" : "2018-01-09", "count" : 1 }

此代码与前面的示例相同,但有一点不同。在这里,我们首先使用 $toLong 将 datetime 字段从字符串类型转换为数字类型,然后使用转换后的值使用 $toDate 转换为日期。

三、当时间戳为对象类型时,将时间戳转换为日期

示例代码(用于 collection3):

// MongoDB 5.0.8 > db.collection3.insertMany([ {"_id":1, "datetime": new Timestamp()}, {"_id":2, "datetime": new Timestamp()}, {"_id":3, "datetime": new Timestamp()}, {"_id":4, "datetime": new Timestamp()}, {"_id":5, "datetime": new Timestamp()} ]); > db.collection3.find();

输出:

{ "_id" : 1, "datetime" : Timestamp(1655448393, 1) } { "_id" : 2, "datetime" : Timestamp(1655448393, 2) } { "_id" : 3, "datetime" : Timestamp(1655448393, 3) } { "_id" : 4, "datetime" : Timestamp(1655448393, 4) } { "_id" : 5, "datetime" : Timestamp(1655448393, 5) }

检查 datetime 字段的类型:

// MongoDB 5.0.8 > typeof db.collection3.findOne().datetime;

输出:

object

这次我们可以使用以下解决方案将时间戳转换为日期并计算每个日期的条目。

示例代码(用于 collection3):

// MongoDB 5.0.8 > db.cybertron_datasource_execution_record.aggregate([ { "$match":{ "dataSourceId": { $in: [77, 83] }, "requestTime": { $gte: 1609430400000, $lt: 1689436800000 } } }, { "$project": { "dataSourceId": "$dataSourceId", "_id":"$requestTime" } }, { "$group": { "_id": { "dataSourceId": "$dataSourceId", "restTime": { "$dateToString": { "format": "%Y-%m", "date": {"$add":[new Date(0),"$_id"]} } } }, "count": { "$sum": 1 } } } ]);

输出:

2023-07 525 2023-06 213 2023-05 321


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有