如何使用RMongo发送多个文档 [英] How to send multiple documents using RMongo

查看:108
本文介绍了如何使用RMongo发送多个文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循 http://docs.mongodb的约定.org/manual/reference/method/db.collection.insert/
在一次调用RMongo :: dbInsertDocument中发送一批多个文档.

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)  

给出以下结果:

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

然后

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

返回以下错误:

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

请注意,如果我替换

L = L[[1]

然后

cat(dataJSON)  

给出以下结果:

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

对dbInsertDocument的相同调用没有错误(并且确实将数据发送到数据库)

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

推荐答案

有人知道吗?我真的希望有一个更好的方法,但是现在只是遍历列表(不理想)

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

这篇关于如何使用RMongo发送多个文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆