在 mongodb 中将时间戳转换为日期-ag捕鱼王app官网

当前位置:ag捕鱼王app官网 > > 数据库 > mongodb >

在 mongodb 中将时间戳转换为日期

作者:迹忆客 最近更新:2023/03/17 浏览次数:

本文演示了如何在 mongodb 中将时间戳转换为日期。它还说明了如何计算特定日期的条目。

在 mongodb 中将时间戳转换为日期

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

我们可以在 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 采用 timestampdateobjectid,根据用户指定的格式进行进一步转换,而 $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

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

示例代码(用于 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 }

此代码与前面的示例相同,但有一点不同。在这里,我们首先使用 $tolongdatetime 字段从字符串类型转换为数字类型,然后使用转换后的值使用 $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

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

示例代码(用于 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 }

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

在 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 进行开始查询。 它为查询中的模式匹配字符串提供正则表达式功能。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图