gpt4 book ai didi

Mongodb 集合对象总小时数集合

转载 作者:行者123 更新时间:2023-12-04 08:47:16 25 4
gpt4 key购买 nike

mongodb 收集数据(提前感谢)

{
"_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
}
我们需要像总持续时间这样的输出来计算以上三个对象 持续时间:((09:30)+(09:30)+(09:30)) ,
输出就好了。 总时长:28 小时:30 分钟

最佳答案

我的建议是这样的:

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 天。

关于Mongodb 集合对象总小时数集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64250761/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com