在 MongoDB 中将时间戳转换为日期
本文演示了如何在 MongoDB 中将时间戳转换为日期。它还说明了如何计算特定日期的条目。
在 MongoDB 中将时间戳转换为日期
从 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.collection3.aggregate([
{
"$project": { "_id": "$datetime" }
},
{
"$group": {
"_id": { "$dateToString": { "format": "%Y-%m-%d", "date": "$_id" } },
"count": { "$sum": 1 }
}
}
]);
输出:
{ "_id" : "2022-06-17", "count" : 5 }
相关文章
在 MongoDB Shell 中列出所有数据库
发布时间:2023/05/11 浏览次数:180 分类:MongoDB
-
交互式 Mongo Shell 提供了多个用于获取数据的选项。 本文介绍了在 Mongo Shell 中列出数据库的几种不同方法。
MongoDB 中检查字段包含的字符串
发布时间:2023/05/11 浏览次数:1024 分类:MongoDB
-
这篇文章解决了如何在 MongoDB 中使用正则表达式来确定字段是否包含字符串。在 MongoDB 中使用正则表达式 正则表达式 (regex) 是定义搜索模式的文本字符串。
在 MongoDB 中 upsert 更新插入
发布时间:2023/05/11 浏览次数:214 分类:MongoDB
-
在 MongoDB 中,upsert 结合了更新和插入命令。 它可以在 update() 和 findAndModify() 操作中使用。MongoDB 中的 upsert 查询 upsert 采用单个布尔参数。
如何卸载 MongoDB
发布时间:2023/05/11 浏览次数:745 分类:MongoDB
-
要从您的计算机中卸载 MongoDB,您必须先删除 MongoDB 服务、数据库和日志文件。使用这篇 MongoDB 文章,您将能够从 Ubuntu Linux、Mac 和 Windows 卸载 MongoDB。 请务必保留数据备份,因为一旦卸载,便
在 MongoDB 中存储日期和时间
发布时间:2023/05/11 浏览次数:762 分类:MongoDB
-
本 MongoDB 教程解释了 Date() 对象是什么以及如何使用 Date() 方法对集合进行排序。 这也将帮助您找到在 MongoDB 中显示和存储日期/时间的最佳方法。
MongoDB 按 ID 查找
发布时间:2023/05/11 浏览次数:1856 分类:MongoDB
-
MongoDB 中的 find by Id() 函数用于获取与用户提供的 id 相匹配的文档。 如果找不到与指定 ID 匹配的文档,则返回空值。
检查 MongoDB 服务器是否正在运行
发布时间:2023/05/11 浏览次数:247 分类:MongoDB
-
这篇 MongoDB 教程将告诉您如何检查是否安装了 MongoDB 以及安装的 MongoDB 服务器的版本。 它在 Windows、UBUNTU 和 MAC 等不同的操作系统中实现。
MongoDB 中的分页
发布时间:2023/05/11 浏览次数:174 分类:MongoDB
-
这篇文章将介绍什么是 MongoDB 中的分页。 为什么在 MongoDB 中需要分页以及在 MongoDB 中完成分页的不同方法或方式是什么。
MongoDB 从查询开始
发布时间:2023/05/11 浏览次数:186 分类:MongoDB
-
在这篇 MongoDB 文章中,用户将学习如何使用 $regex 进行开始查询。 它为查询中的模式匹配字符串提供正则表达式功能。