如何计算 mongodb 中的时间戳差异(以小时为单位)? [英] How to calculate timestamp difference in mongodb (in hours)?
本文介绍了如何计算 mongodb 中的时间戳差异(以小时为单位)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的数据:
[
{
id: 1,
starttime: ISODate("2015-08-24T00:00:00.000Z"),
endtime: ISODate("2015-08-24T07:00:00.000Z")
},
{
id: 2,
starttime: ISODate("2015-08-24T20:00:00.000Z"),
endtime: ISODate("2015-08-25T01:00:00.000Z")
}
]
我可以做一个 mongodb 查询来显示 starttime 和 endtime 的持续时间(或者在这种情况下是差异操作),结果如下:
can I make a mongodb query to display duration (or in this case a difference operation) of starttime and endtime with results like:
[ {id:1, duration: 7}, {id: 2, duration: 5}]
请注意,时间戳可以有不同的日期,因此 $hour
(聚合管道)可能不起作用.任何人都可以帮忙吗?谢谢
Notice that timestamp can have different date so $hour
(aggregation pipeline) might not work. Can anyone help ? Thank you
推荐答案
db.collectionname.aggregate([
{$project: {
duration: {$divide: [{$subtract: ["$endtime", "$starttime"]}, 3600000]}
}}
])
这篇关于如何计算 mongodb 中的时间戳差异(以小时为单位)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文