我正在编写一个 RScript 来将当前系统时间戳与 MongoDB 最后更新的时间戳进行比较。 我的做法:
db.runs.find().sort({'id':-1}).limit(1)
- 给出最后更新记录的 objectidObjectId("5b27f3957cf77b51d60c1502").getTimeStamp()
- 输出:ISODate("2018-06-18T18:01:57Z")
可以在R中编写这些步骤的脚本,以便我可以将此 ISODate 与当前系统时间戳进行比较吗?或者还有其他方法可以达到这个要求吗?
最佳答案
这个功能对我有用:
library(base)
get_date_from_objectid <- function(oid) {
#Get first 8 Hexa characters from ObjectId corresponding with the datetime:
datetime_Hex <- paste("0x", substr(oid, 0, 8), sep = "")
#Convert from Hexa to Decimal using base::strtoi():
datetime_Decimal <- strtoi(c(datetime_Hex))
#Get the date from de the decimal value:
date = as.Date.POSIXct(datetime_Decimal, origin="1970-01-01")
return(date)
}
#Example
objectId= "5dfce6ad859e645780f88b53"
get_date_from_objectid(objectId)
#This will return "2019-12-20"
关于r - MongoDB-ISODate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50934298/