I am trying to calculate the difference between two timestamps in mongodb.
我试图计算mongodb中两个时间戳之间的差异。
With MySQL it can easily be achieved using this query
使用MySQL,可以使用此查询轻松实现
SELECT ROUND((TIME_TO_SEC(NOW()) - TIME_TO_SEC(lastseen))/60) AS minutes
where lastseen is a timestamp column.
SELECT ROUND((TIME_TO_SEC(NOW()) - TIME_TO_SEC(lastseen))/ 60)AS分钟,其中lastseen是时间戳列。
Here is my schema:
这是我的架构:
{
"_id" : ObjectId("5aa329cb0b717a0f637b0937"),
"username" : "admin@mans.com",
"token" : "1085bbc68a",
"realname" : "Administrator",
"lastseen" : ISODate("2018-03-09T19:41:47.552Z")
}
How can I get the time difference between lastseen
in the document and datetime.datetime.now()
in minutes?
如何获得文档中lastseen和datetime.datetime.now()之间的时差(以分钟为单位)?
1 个解决方案
#1
0
The $subtract
aggregation query operator can be used to find the difference of two dates. e.g
$ subtract聚合查询运算符可用于查找两个日期的差异。例如
query = [{
'$project': {
'username': 1,
'date_diff_mins': {
'$divide': [
{'$subtract': [datetime.now(), '$lastseen']},
1000 * 60
]
}
}
}]
db.collection.aggregate(query)
#1
0
The $subtract
aggregation query operator can be used to find the difference of two dates. e.g
$ subtract聚合查询运算符可用于查找两个日期的差异。例如
query = [{
'$project': {
'username': 1,
'date_diff_mins': {
'$divide': [
{'$subtract': [datetime.now(), '$lastseen']},
1000 * 60
]
}
}
}]
db.collection.aggregate(query)