CNUB logo Huntington Xavier University
Family Dollar
UDF Dairy Farmers Kroger groceries American Telegraph and Telephone WEBN home page
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:

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



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 ( {
	b <- mongo.cursor.value(cursor)
	name[i] <- mongo.bson.value(b, "name")
	age[i] <- mongo.bson.value(b, "age")
	i <- i + 1
df <-, 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")

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

I'll probably add a function for it in the next release. All three of the MongoDB Drivers I have written now provide the mongo.distinct() function.


Contact Info

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


MongoDB Drivers

I also wrote related MongoDB drivers for both Delphi and Matlab. See MongoDelphiDriver and MongoMatlabDriver.



2012-03-07: rmongodb version 1.0.3 released. This release features a slightly reworked mongo-c-driver that is able to interface to each of my MongoDB Drivers without changes. mongo.distinct() was also added while I was doing the required changes to each driver.

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 See the docs for more information: rmongodb.pdf.