CNUB logo Huntington Xavier University
Family Dollar
McDonald's
UDF Dairy Farmers Kroger groceries American Telegraph and Telephone WEBN home page

Page History: rmongodb - R Driver for MongoDB

Compare Page Revisions



« Older Revision - Back to Page History - Newer Revision »


Page Revision: 2012/02/22 08:20


rmongodb is a client driver for the R language to MongoDB. The development of this driver was sponsored by 10Gen, Inc..

The full reference manual is available here: rmongodb.pdf

The source code for this driver is now publicly available at GitHub here: https://github.com/gerald-lindsly/rmongodb.

rmongodb is now on the Comprehensive R Archive Network (CRAN) for pre-built distribution.

FAQ

How do I query a database and put the result in a data frame?

Here's an example.  Modify it for your purposes.

count <- mongo.count(mongo, ns, query)
cursor <- mongo.find(mongo, query)
name <- vector("character", count)
age <- vector("numeric", count)
i <- 1
while (mongo.cursor.next(cursor)) {
	b <- mongo.cursor.value(cursor)
	name[i] <- mongo.bson.value(b, "name")
	age[i] <- mongo.bson.value(b, "age")
	i <- i + 1
}
df <- as.data.frame(list(name=name, age=age))

How do I get the set of distinct keys in a collection?

rmongodb does not directly support distinct but it can be easily implemented
like so:
mongo.distinct <- function(mongo, db, collection, key) {
   b <- mongo.command(mongo, db, list(distinct=collection, key=key))
   if (!is.null(b))
      b <- mongo.bson.value(b, "values")
   b
}

names <- mongo.distinct(mongo, "test", "people", "name")

I'll probably add a function for it in the next release.

Contact Info

Please contact me at if you have comments, issues or bug reports concerning the driver.

MongoMatlabDriver

I also wrote a related MongoDB driver for Matlab. See MongoMatlabDriver.

News

2011-09-08: Append and value functions now support the dim attribute. This involved some tricky code that I pondered on for a while. Previously, the only way to handle the dim attribute was through mongo.bson.buffer.append.object(). Now, multidimensional arrays are handled in a 'natural' way. This allows import from foreign applications (non-R) and export to them as well. Also, the output is not cluttered with the 'R_OBJ' flag.

2011-09-05: I added mongo.bson.buffer.append.object() last night. This allows higher level R objects to be stored in the database without loss of attributes (such as their class). The other 'append' functions lose most attributes as they are primarily targeted towards storing single values (or vectors of them). These do support the 'names' attribute as this fitted in naturally with BSON. mongo.bson.buffer.append.object() puts a wrapper around an object's lower level value which can be detected by mongo.bson.value(), mongo.bson.iterator.value and mongo.bson.to.list(). See the docs for more information: rmongodb.pdf.