gpt4 book ai didi

r - MongoDB-ISODate

转载 作者:行者123 更新时间:2023-12-03 15:59:41 24 4
gpt4 key购买 nike

我正在编写一个 RScript 来将当前系统时间戳与 MongoDB 最后更新的时间戳进行比较。我的做法:

  1. db.runs.find().sort({'id':-1}).limit(1) - 给出最后更新记录的 objectid
  2. ObjectId("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/

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