简体   繁体   English

从R中插入mongodb中的json date obeject

[英]Inserting json date obeject in mongodb from R

I am trying to insert forecasted values from a forecasting model along with timestamps in mongodb from. 我试图从预测模型中插入预测值以及mongodb中的时间戳。

The following code converts the R dataframe into json and then bson. 以下代码将R数据帧转换为json,然后转换为bson。 However,when the result is inserted into mongodb, the timestamp is not recognized as date object. 但是,将结果插入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)

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
    }]

You can use library(mongolite) to insert dates correctly for you. 您可以使用library(mongolite)为您正确插入日期。 However, I've only managed to get it to correctly insert dates using data.frames . 但是,我只是设法让它使用data.frames正确插入日期。 It fails to insert dates correctly using lists or JSON strings. 它无法使用listsJSON字符串正确插入日期。

Here is a working example using a data.frame to insert the data. 这是一个使用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()

在此输入图像描述

A potential (but less than ideal) solution could be to coerce your list to a data.frame and then insert that. 一个潜在的(但不太理想的)解决方案可能是将您的列表强制转换为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)) 

Or alternatively, insert your list, and then write a function to convert ts values to ISODates() directly within mongo 或者,插入列表,然后编写一个函数,直接在mongo ISODates() ts值转换为ISODates()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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