Mongodb 集合对象总小时数集合 [英] Mongodb collection objects total hours collection

查看:29
本文介绍了Mongodb 集合对象总小时数集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

mongodb 收集数据(提前感谢)

mongodb collection data (thanks advance for this)

{
    "_id" : ObjectId("5f7b3d78e95af70e17efd6d6"),
    "employeeId" : "2707",
    "employeeName" : "HrJosh",
    "status" : "Present",
    "date" : "2020-10-16",
    "clientName" : "Lorent",
    "projectName" : "ChatBot",
    "taskName" : "Learning",
    "durationHrs" : "09:30",
    "createdAt" : ISODate("2020-10-05T15:36:24.677Z"),
    "updatedAt" : ISODate("2020-10-05T15:36:24.677Z"),
    "__v" : 0
}
{
    "_id" : ObjectId("5f7b5c3de95af70e17efd6d7"),
    "employeeId" : "2707",
    "employeeName" : "Gowdham",
    "status" : "Present",
    "date" : "2020-10-01",
    "clientName" : "Lorent",
    "projectName" : "ChatBot",
    "taskName" : "Meeting",
    "durationHrs" : "09:30",
    "createdAt" : ISODate("2020-10-05T17:47:41.381Z"),
    "updatedAt" : ISODate("2020-10-05T17:47:41.381Z"),
    "__v" : 0
}
{
    "_id" : ObjectId("5f7b5c62e95af70e17efd6d8"),
    "employeeId" : "2707",
    "employeeName" : "Gowdham",
    "status" : "Present",
    "date" : "2020-10-02",
    "clientName" : "fsdsdsd",
    "projectName" : "ChatBotsd",
    "taskName" : "Manage Team",
    "durationHrs" : "09:30",
    "createdAt" : ISODate("2020-10-05T17:48:18.994Z"),
    "updatedAt" : ISODate("2020-10-05T17:48:18.994Z"),
    "__v" : 0
}

我们需要像总持续时间这样的输出来计算以上三个对象 durationHrs:((09:30)+(09:30)+(09:30)) ,输出就好了.总时长:28 小时:30 分钟

we need output like total duration hours for calculate above three objects durationHrs:((09:30)+(09:30)+(09:30)) , Output be like. Total Duration Hour : 28hrs:30mins

推荐答案

我的建议是这样的:

db.collection.aggregate([
   {
      $addFields: {
         duration: {
            $toLong: {
               $dateFromParts: {
                  year: 1970,
                  hour: { $toInt: { $first: { $split: ["$durationHrs", ":"] } } },
                  minute: { $toInt: { $last: { $split: ["$durationHrs", ":"] } } },
               }
            }
         }
      }
   },
   { $group: { _id: null, durations: { $sum: "$duration" } } },
   {
      $set: {
         h: {
            $toInt: {
               $sum: [{
                  $multiply: [
                     { $dayOfYear: { $toDate: "$durations" } }, 24]
               },
               -24,
               { $hour: { $toDate: "$durations" } }]
            }
         },
         m: { $minute: { $toDate: "$durations" } }
      }
   },
   { $project: { durations: { $concat: [{ $toString: "$h" }, ":", { $toString: "$m" }] } } }
])

请注意,它的有效期最长为一年,即 366 天.

Be aware, it works only for duration's up to one year, i.e. 366 days.

这篇关于Mongodb 集合对象总小时数集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆