我正在尝试从预测模型中插入预测值以及来自mongodb的时间戳。
以下代码将R数据帧转换为json,然后转换为bson。但是,将结果插入mongodb时,时间戳不会被识别为日期对象。
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)
实际输出:
{
"_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"
]
}
}
预期输出格式:
"_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
}]
答案 0 :(得分:0)
您可以使用library(mongolite)
为您正确插入日期。但是,我只是设法使用data.frames
正确插入日期。它无法使用lists
或JSON
字符串正确插入日期。
以下是使用data.frame
插入数据的工作示例。
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()
潜在(但不太理想)的解决方案可能是将您的列表强制转换为data.frame
,然后插入该列表。
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))
或者,插入列表,然后编写一个函数,将ts
值直接转换为ISODates()
mongo