gpt4 book ai didi

node.js - 如何在mongodb中减去两个日期时间

转载 作者:可可西里 更新时间:2023-11-01 10:00:05 25 4
gpt4 key购买 nike

我用过聚合函数。

db.checkins.aggregate([
{$match: {checkinType: "Beacon",
"associationIds.organizationId":"af39bc69-1938-4149",
"checkinData.time": {"$gte": new Date("2018-01-18T18:30:00.000Z"),
"$lt": new Date("2018-01-19T18:30:00.000Z")}
}
},
{"$sort":{"checkinData.time":-1}},
{$group: {"_id":
{"orgId":"$asst.organizationId", "userId":"$asst.userId"},
"lastSeen":{"$first":"$checkinData.time"},
"firstSeen":{"$last":"$checkinData.time"},
}
},
{"$project":{"_id":1,"lastSeen":1, "firstSeen":1,
totalHourSpent:{$subtract: ["$lastSeen","$firstSeen"]}}},
])

When I performed this Query mongo return totalHourSpent in milisecond which is given below.

{
"_id" : {
"orgId" : "af39bc69-1938-4149-b9f7-f101fd9baf73",
"userId" : "34adb4a0-0012-11e7-bf32-cf79d6b423e9"
},
"lastSeen" : ISODate("2018-01-19T18:49:52.242+05:30"),
"firstSeen" : ISODate("2018-01-19T10:08:21.026+05:30"),
"totalHourSpent" : NumberLong("31291216")
},
{
"_id" : {
"orgId" : "af39bc69-1938-4149-b9f7-f101fd9baf73",
"userId" : "679416b0-3f88-11e7-8d27-77235eb1ba9b"
},
"lastSeen" : ISODate("2018-01-19T20:51:30.946+05:30"),
"firstSeen" : ISODate("2018-01-19T11:07:44.256+05:30"),
"totalHourSpent" : NumberLong("35026690")
},

How to calculate totalHourSpent in hour. Thanks in advance.

最佳答案

$subtract 以毫秒为单位提供持续时间。所以我们需要将持续时间除以 3600000 作为小时格式。

返回的 mills 可以通过除以 3600000(1 小时内的毫秒数)转换为小时:

totalHourSpent:{$divide : [{$subtract: ["$lastSeen","$firstSeen"]}, 3600000]}

35026690÷3600000=9.72963611111111 hours

关于node.js - 如何在mongodb中减去两个日期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48369419/

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