How to send multiple documents using RMongo

限于喜欢 提交于 2019-12-13 16:11:53

问题


I am following the conventions from http://docs.mongodb.org/manual/reference/method/db.collection.insert/
to send a batch of multiple documents in one call of RMongo::dbInsertDocument.

data=data.frame(A=c(1,2),B=c(3,4))  
L=lapply(split(data,rownames(data)),as.list)  
names(L)=NULL  
dataJSON = toJSON(L)  
cat(dataJSON)  

which gives the following result:

[  
 {  
  "A":1,
  "B":3
 },
 {
  "A":2,
  "B":4
 }
]

Then

dbInsertDocument(rmongo.object=myRmongo.object, collection=myCollection, doc=dataJSON)

returns the following error:

Error in ls(envir = envir, all.names = private) :  
invalid 'envir' argument

Note that if I replace

L = L[[1]

Then

cat(dataJSON)  

gives the following result:

{  
 "A":1,
 "B":3
}

and the same call to dbInsertDocument works with no error (and the data is indeed sent to the database)


回答1:


Has anyone figured this out? I would really like a better way to do this, but for now am just looping over the list (not ideal)

data=data.frame(A=c(1,2),B=c(3,4))  
L=lapply(split(data,rownames(data)),as.list)  
names(L)=NULL  
for (i in 1:NROW(L)) {
    dataJSON = toJSON(L[[i]])  
    output <- dbInsertDocument(mongo, "test_data7", dataJSON)
}


来源:https://stackoverflow.com/questions/19564321/how-to-send-multiple-documents-using-rmongo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!