r - How to query date data with millisecond through mongolite? -
as below, type of 'stime' , 'etime' 'date' millisecond:
{ "_id" : objectid("58d843dd4da8fc62c8c6a0bd"), "stime" : isodate("2017-03-26t22:21:34.923z"), "etime" : isodate("2017-03-26t22:42:17.341z"), }
and queried data this:
data.names<-c("stime","etime") mongo.data <- mongo(collection = "data_1",db = "data_test", url = "mongodb://10.23.102.122:32800") journey <- mongo.data$find(query = '{\"_id\" : {\"$oid\":\"58d843dd4da8fc62c8c6a0bd\"}}', fields = paste('{\"_id\":true, ', paste('\"',data.names,'\":true', collapse = ', ', sep=''), '}', sep = ''))
but data queried utc time-stamp without millisecond data:
> journey _id stime etime 1 58d843dd4da8fc62c8c6a0bd 1490566894 1490568137 > format(as.posixct(unlist(journey[1,-1]), origin = "1970-01-01 00:00:00"), format="%y-%m-%d %h-%m-%os3") stime etime "2017-03-27 06-21-34.000" "2017-03-27 06-42-17.000"
so, how can query data millisecond data?
i've found solution via mongo aggregate method:
journey <- mongo.data$aggregate(pipeline = '[{\"$match\":{\"_id\":{\"$oid\":\"58d843dd4da8fc62c8c6a0bd\"}}}, {\"$project\":{\"_id\":1, \"stime\":1, \"stime_milliseconds\":{\"$millisecond\":\"$stime\"}, \"etime\":1, \"etime_milliseconds\":{\"$millisecond\":\"$etime\"}}}]')
in code above, extracted millisecond data of 'stime' , 'etime' 2 new variables 'stime_milliseconds' , 'etime_milliseconds' using 'millisecond' method in mongo. extract data mongo via r, this:
> journey _id stime etime stime_milliseconds etime_milliseconds 1 58d843dd4da8fc62c8c6a0bd 1490566894 1490568137 923 341
the last 2 numbers stands millisecond data.
this solution, , welcome better answer
forgive poor english~()~
Comments
Post a Comment