rmongodb – R Driver for MongoDB

September 3, 2011
By
The source code to rmongodb (home page at http://cnub.org/rmongodb.ashx), a driver to MongoDB for the R language, has been released as open source at GitHub: https://github.com/gerald-lindsly/rmongodb.  This portable full-featured package was developed on top of the mongodb.org supported C driver. It runs almost entirely in native code so you can expect high performance.  Plans are to submit rmongodb to CRAN soon for pre-built binary distribution, but first I would like some feedback from some real world usage by others.  All functions are thoroughly documented and include examples.  There is a also a sample application included, teachers_aid.R, demonstrating how a simple relational database can be implemented using MongoDB and rmongodb.
Connecting to a MongoDB server running on localhost is as straight forward and simple as:
mongo <- mongo.create()
This creates a “mongo” classed object which you’ll use for subsequent communication with MongoDB.
Once you have established the connection, you may execute CRUD operations on the database quite easily. Simplified prototypes for these look like this:
mongo.insert(mongo, namespace, record)
result <- mongo.find.one(mongo, namespace, query)
mongo.update(mongo, namespace, criteria, objNew)
mongo.remove(mongo, namespace, criteria)
namespace is the name of the collection on which to perform the operation.
MongoDB and rmongodb use BSON documents to represent objects in the database and for network messages.  BSON is an efficient binary representation of JSON-like objects. See http://www.mongodb.org/display/DOCS/BSON.
There are numerous functions in rmongodb for serializing/deserializing data to/from BSON.  In fact, much of this is done transparently when you use R lists to describe records, queries and criteria.
A simple example of an actual insert looks like this:
mongo.insert(mongo, “test.people”, list(name=”John”, age=34L, 
    address=”1033 Vine Street”))
There is full support for vectors of atomic types (including complex and raw):
record <- list(fruits=c(“apple”, “orange”, “banana”))
mongo.insert(mongo, “test.misc”, record)
Lists of lists of vectors are recursively supported also.
I won’t bore you with further details as the documentation is complete and thorough.  Seehttp://cnub.org/doc/rmongodb.pdf for the full reference manual.
Please contact me at [email protected] if you have any questions, comments, or bug reports, or if you need support for the driver.
—————
This has been a guest post by Gerald Lindsly


If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.