mongodb中如何将字符串时间格式转换为日期或时间

发布于 2025-01-14 18:11:12 字数 275 浏览 1 评论 0原文

我在转换字符串时间格式时遇到问题,即 0h 0m 53s+695ms 到日期或时间 我有一个具有这种格式的持续时间字段,并且类型为 string ,我必须添加集合中与特定条件匹配的所有记录的持续时间。

但我收到如下错误: 无法解析 $convert 中的数字“0h 0m 53s+695ms”,且没有 onError 值:解析 0h 0m 53s+695ms 时出现错误的数字“h”(注意:如果我指定 onError,则结果只会填充错误条件) 诸如此类的还有很多。

关于如何将此特定类型转换为数据的任何线索?

I am facing issue while converting string time format i.e., 0h 0m 53s+695ms
to date or time
I have a duration field with this format and is of type string , I have to add the duration of all the records in the collection matching on certain condition.

But I am getting error like :
Failed to parse number '0h 0m 53s+695ms' in $convert with no onError value: Bad digit "h" while parsing 0h 0m 53s+695ms" (Note: if I specify onError than the result only get populates with error conditions)
and many more of such kind.

Any clue on how to convert this specific type to data?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风筝在阴天搁浅。 2025-01-21 18:11:12

该聚合管道会将字符串转换为秒数。您可以总结一下:

db.collection.aggregate([
   { $set: { t: { $first: { $regexFindAll: { input: "$t", regex: /^(\d+)h (\d+)m (\d+)s\+(\d+)ms$/ } } } } },
   {
      $set: {
         t: {
            $sum: [
               { $multiply: [{ $toInt: { $arrayElemAt: ["$t.captures", 0] } }, 60 * 60] },
               { $multiply: [{ $toInt: { $arrayElemAt: ["$t.captures", 1] } }, 60] },
               { $toInt: { $arrayElemAt: ["$t.captures", 2] } },
               { $divide: [{ $toInt: { $arrayElemAt: ["$t.captures", 2] } }, 1000] }
            ]
         }
      }
   }
])

您可以将输出格式化为如下形式:

db.collection.aggregate([
   { $set: { t: { $first: { $regexFindAll: { input: "$t", regex: /^(\d+)h (\d+)m (\d+)s\+(\d+)ms$/ } } } } },
   {
      $set: {
         t: {
            $sum: [
               { $multiply: [{ $toInt: { $arrayElemAt: ["$t.captures", 0] } }, 60 * 60] },
               { $multiply: [{ $toInt: { $arrayElemAt: ["$t.captures", 1] } }, 60] },
               { $toInt: { $arrayElemAt: ["$t.captures", 2] } },
               { $divide: [{ $toInt: { $arrayElemAt: ["$t.captures", 2] } }, 1000] }
            ]
         }
      }
   },
   {
      $set: {
         t: {
            $dateToString: {
               date: { $toDate: { $multiply: ["$t", 1000] } },
               format: "%H:%M:%S.%L"
            }
         }
      }
   }
])

This aggregation pipeline will convert the string into number of seconds. These you can sum up:

db.collection.aggregate([
   { $set: { t: { $first: { $regexFindAll: { input: "$t", regex: /^(\d+)h (\d+)m (\d+)s\+(\d+)ms$/ } } } } },
   {
      $set: {
         t: {
            $sum: [
               { $multiply: [{ $toInt: { $arrayElemAt: ["$t.captures", 0] } }, 60 * 60] },
               { $multiply: [{ $toInt: { $arrayElemAt: ["$t.captures", 1] } }, 60] },
               { $toInt: { $arrayElemAt: ["$t.captures", 2] } },
               { $divide: [{ $toInt: { $arrayElemAt: ["$t.captures", 2] } }, 1000] }
            ]
         }
      }
   }
])

The output you can format like this:

db.collection.aggregate([
   { $set: { t: { $first: { $regexFindAll: { input: "$t", regex: /^(\d+)h (\d+)m (\d+)s\+(\d+)ms$/ } } } } },
   {
      $set: {
         t: {
            $sum: [
               { $multiply: [{ $toInt: { $arrayElemAt: ["$t.captures", 0] } }, 60 * 60] },
               { $multiply: [{ $toInt: { $arrayElemAt: ["$t.captures", 1] } }, 60] },
               { $toInt: { $arrayElemAt: ["$t.captures", 2] } },
               { $divide: [{ $toInt: { $arrayElemAt: ["$t.captures", 2] } }, 1000] }
            ]
         }
      }
   },
   {
      $set: {
         t: {
            $dateToString: {
               date: { $toDate: { $multiply: ["$t", 1000] } },
               format: "%H:%M:%S.%L"
            }
         }
      }
   }
])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文