简体   繁体   中英

MongoDB - ISODate

I am writing a RScript to compare the current system timestamp with the MongoDB last updated timestamp. My approach:

  1. db.runs.find().sort({'id':-1}).limit(1) - gives objectid of last updated record
  2. ObjectId("5b27f3957cf77b51d60c1502").getTimeStamp() - Output: ISODate("2018-06-18T18:01:57Z")

Can these steps be scripted in R so that I can compare this ISODate with the current system timestamp? Or is there any other way to achieve this requirement?

This function works to me:

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM