Inserting json date obeject in mongodb from R

1.1k Views Asked by At

I am trying to insert forecasted values from a forecasting model along with timestamps in mongodb from.

The following code converts the R dataframe into json and then bson. However,when the result is inserted into mongodb, the timestamp is not recognized as date object.

mongo1 <-mongo.create(host = "localhost:27017",db = "test",username = "test",password = "test")
rev<-data.frame(ts=c("2017-01-06 05:30:00","2017-01-06 05:31:00","2017-01-06 05:32:00","2017-01-06 05:33:00","2017-01-06 05:34:00"),value=c(10,20,30,40,50))
rev$ts<-as.POSIXct(strptime(rev$ts,format = "%Y-%m-%d %H:%M:%S",tz=""))

revno<-"Revision1"

mylist <- list()
mylist[[ revno ]] <- rev
mylist["lastRevision"]<-revno

StartTime<-"2017-01-06 05:30:00"

site<-"Site1"
id <- mongo.bson.buffer.create()
mongo.bson.buffer.append(id, "site",site)
mongo.bson.buffer.append(id, "ts",as.POSIXct(strptime(StartTime,format = "%Y-%m-%d %H:%M:%S",tz="")) )
s <- mongo.bson.from.buffer(id)

rev.json<-toJSON(mylist,POSIXt=c("mongo"))
rev.bson<-mongo.bson.from.JSON(rev.json)


actPower <- mongo.bson.buffer.create()
mongo.bson.buffer.append(actPower, "_id",s)
mongo.bson.buffer.append(actPower,"activePower",rev.bson)
x <- mongo.bson.from.buffer(actPower)
x

mongo.insert(mongo1,'solarpulse.forecast',x)

Actual Output:

{
    "_id" : {
        "site" : "site1",
        "ts" : ISODate("2017-01-06T18:30:00Z")
    },
    "activePower" : {
        "Revision1" : [
            {
                "ts" : 1483660800000,
                "value" : 10
            },
            {
                "ts" : 1483660860000,
                "value" : 20
            },
            {
                "ts" : 1483660920000,
                "value" : 30
            },
            {
                "ts" : 1483660980000,
                "value" : 40
            },
            {
                "ts" : 1483661040000,
                "value" : 50
            }
        ],
        "lastRevision" : [
            "Revision1"
        ]
    }
}

Expected Output format:

"_id" : {
        "site" : "test",
        "ts" : ISODate("2016-12-18T18:30:00Z")
    }

"Revision1": [{
        "ts": ISODate("2016-12-19T07:30:00Z"),
        "value": 31
    }, {
        "ts": ISODate("2016-12-19T07:45:00Z"),
        "value": 52
    }, {
        "ts": ISODate("2016-12-19T08:00:00Z"),
        "value": 53
    }, {
        "ts": ISODate("2016-12-19T08:15:00Z"),
        "value": 30
    }, {
        "ts": ISODate("2016-12-19T08:30:00Z"),
        "value": 43
    }, {
        "ts": ISODate("2016-12-19T08:45:00Z"),
        "value": 31
    }, {
        "ts": ISODate("2016-12-19T09:00:00Z"),
        "value": 16
    }, {
        "ts": ISODate("2016-12-19T09:15:00Z"),
        "value": 39
    }, {
        "ts": ISODate("2016-12-19T09:30:00Z"),
        "value": 17
    }, {
        "ts": ISODate("2016-12-19T09:45:00Z"),
        "value": 45
    }, {
        "ts": ISODate("2016-12-19T10:00:00Z"),
        "value": 60
    }, {
        "ts": ISODate("2016-12-19T10:15:00Z"),
        "value": 39
    }, {
        "ts": ISODate("2016-12-19T10:30:00Z"),
        "value": 46
    }, {
        "ts": ISODate("2016-12-19T10:45:00Z"),
        "value": 57
    }, {
        "ts": ISODate("2016-12-19T11:00:00Z"),
        "value": 29
    }, {
        "ts": ISODate("2016-12-19T11:15:00Z"),
        "value": 7
    }]
1

There are 1 best solutions below

0
On

You can use library(mongolite) to insert dates correctly for you. However, I've only managed to get it to correctly insert dates using data.frames. It fails to insert dates correctly using lists or JSON strings.

Here is a working example using a data.frame to insert the data.

library(mongolite)

m <- mongo(collection = "test_dates", db = "test", url = "mongodb://localhost")

# m$drop()

df <- data.frame(id = c("site1","site2"),
                 ts = c(Sys.time(), Sys.time()))

m$insert(df)
#Complete! Processed total of 2 rows.
#$nInserted
#[1] 2
#
#$nMatched
#[1] 0
#
#$nRemoved
#[1] 0
#
#$nUpserted
#[1] 0
#
#$writeErrors
#list()

enter image description here

A potential (but less than ideal) solution could be to coerce your list to a data.frame and then insert that.

rev<-data.frame(ts=c("2017-01-06 05:30:00","2017-01-06 05:31:00","2017-

01-06 05:32:00","2017-01-06 05:33:00","2017-01-06 05:34:00"),value=c(10,20,30,40,50))
rev$ts<-as.POSIXct(strptime(rev$ts,format = "%Y-%m-%d %H:%M:%S",tz=""))

revno<-"Revision1"

mylist <- list()
mylist[[ revno ]] <- rev
mylist["lastRevision"]<-revno

m$insert(data.frame(mylist)) 

Or alternatively, insert your list, and then write a function to convert ts values to ISODates() directly within mongo