1

As below, type of 'stime' and 'etime' is 'Date' with millisecond:

{
"_id" : ObjectId("58d843dd4da8fc62c8c6a0bd"),
"stime" : ISODate("2017-03-26T22:21:34.923Z"),
"etime" : ISODate("2017-03-26T22:42:17.341Z"),
}

And I queried data like 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 the data queried are 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 I query the data with millisecond data?

HUGH_HUGO
  • 31
  • 5
  • Please avoid screenshots if you can use text instead, and please provide a [mcve]. See also http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example –  Mar 28 '17 at 10:59
  • The sceenshots are the outcome of queried data in mongo and R, which are structured data and hard to be converted to text. – HUGH_HUGO Mar 29 '17 at 08:25
  • You could type out the data in either screenshot in about 30 seconds. Make your question a useful resource for others: that's how SO works. –  Mar 29 '17 at 08:48
  • Thanks for your tips, and I've re-edited the description of my problem. So do you have any idea about it? – HUGH_HUGO Mar 31 '17 at 07:51

1 Answers1

2

I've found a 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 the code above, I extracted the millisecond data of 'stime' and 'etime' as two new variables 'stime_milliseconds' and 'etime_milliseconds' using the 'millisecond' method in mongo. Then extract the data from mongo via R, which is like this:

> journey
                       _id      stime      etime stime_milliseconds etime_milliseconds
1 58d843dd4da8fc62c8c6a0bd 1490566894 1490568137                923                341

The last two numbers stands for the millisecond data.

This is my solution, and welcome better answer

Forgive my poor English~()~

HUGH_HUGO
  • 31
  • 5