2

I have a dataframe in R and I am trying to bulk insert each row of the dataframe as separate document in MongoDB. The closest I could do is using following script which creates a document and makes the rows of dataframe as its sub document.

x <- toJSON(unname(split(score, 1:nrow(score))))
bson <- mongo.bson.from.JSON(x)
mongo.insert(mongo,'abc.abc',x)

On the other hand, I want each row as separate document. I also see that above method is very fast but if we loop around the rows, it would highly reduce the speed

Kshitij Dixit
  • 71
  • 1
  • 6
  • I have already posted the same question (but for `Rmongo` instead of `rmongodb`) http://stackoverflow.com/questions/19564321/how-to-send-multiple-documents-using-rmongo. Also I have put a github issue on this https://github.com/tc/RMongo/issues/22 – RockScience Mar 11 '15 at 07:50
  • I see that the question was asked in 2013. Has there been no solution to this since then? – Kshitij Dixit Mar 11 '15 at 09:30

2 Answers2

9

The new mongolite package does this automatically:

library(mongolite)
m <- mongo("iris")
m$insert(iris)
Jeroen Ooms
  • 31,998
  • 35
  • 134
  • 207
  • 1
    WOW, mongolite is awesome! – SteveO7 Oct 13 '15 at 01:13
  • 1
    If you are looking for a way to create a MongoDB collection from an R dataframe, look no farther than mongolite. You basically create a collection object then run methods on it, like 'insert' in Jeroen's example. Super simple and fast. This should be the accepted answer. – SteveO7 Oct 13 '15 at 12:54
3
library(rmongodb)
df <- data.frame(A=c("a","a","b","b"), B=c("X","X","Y","Z"), C=c(1,2,3,4), 
stringsAsFactors = F) 
lst <- split(df, rownames(df))
bson_lst <- lapply(lst, mongo.bson.from.list)
mongo.insert.batch(mongo = mongo, ns = "db.collection", lst = bson_lst)

And please, dont't use mongo.bson.from.JSON, use mongo.bson.from.list instead. It is much more straighforward (and much more faster!) way to convert R object to bson object.

Dmitriy Selivanov
  • 4,545
  • 1
  • 22
  • 38