Drupal Web Application Development Company

call us//
773-525-8255

blog

/Drupal and Mobile web development thoughts
from our Open Source application team

Using MongoDB for Field Storage in Drupal

Tue, 2012-06-12 10:00 -- Brant

In Drupal, there is a suite of modules that allow for developers to take advantage of MongoDB's NoSQL storage.

Using MongoDB for Field Storage in Drupal

MongoDB is a NoSQL database system that stores structured data as JSON-esque documents with dynamic schemas. 

This makes for data integration on some apps much easier and faster. 

In Drupal, there is a suite of modules that allow for developers and site builders to take advantage of this document storage system for things like field storage, block configuration, caching, watchdog (logging) and more. 

Today we're going to look at setting up MongoDB and configuring Drupal to use the mongodb_field_storage module. 

This requires the latest version of Drupal as well as the MongoDB project found at http://drupal.org/project/mongodb

 

Setting Up MongoDB

MongoDB is a breeze to install if you're using a package manager like Homebrew.  For system specific MongoDB install information, visit http://www.mongodb.org/downloads#packages

Once you have MongoDB up and running you should be at a point where you can test it out in your terminal using the following command:

$  mongo

MongoDB shell version: 2.0.4

connecting to: test

By default, the mongo shell should not require any kind of authentication.  For our purposes, we will leave this be. 

 

Configuring Drupal for MongoDB

The next step is to Install and configure Drupal as you normally would.  Before you create any new content types or add fields to existing content types, you'll want to download and install the MongoDB Drupal modules from http://drupal.org/project/mongodb

At this point you'll want to enable the mongodb base module that is a library to support the other modules.  You can also turn on the mongodb_field_storage module.  

We're very close now. The last step in getting MongoDB/Drupal to work together is to edit your Drupal's settings.php file.

Here is the configuration you'll need to add to set up a connection string from Drupal to MongoDB.

$conf['mongodb_connections'] = array(

  'default' => array(

    'host' => 'localhost',

    'db' => 'drupal_default',

  ),

);

This is the basic connection string required for non-authenticated local connection between PHP/Drupal and MongoDB.  You can set the db property to any name you'd like.  Here we are using 'drupal_default' as our database name.

Note:  The 7.x-1.0-rc1 of mongodb module for Drupal requires a small patch to get the connection string working.  This issue should be resolved with newer versions of the module suite, but for now you'll need this patch: http://drupal.org/node/1605616#comment-6058876

Also, you will need to define in settings.php the configuration for field storage.

$conf['field_storage_default'] = 'mongodb_field_storage';

Save settings.php and clear all caches.  

Now you can go into Drupal and set up your content types as you would normally.  This works best if you remove the default content types and start fresh.  Doing so will ensure that the Field API is no longer storing data. Any Field API fields that were set before MongoDB came into the situation will be stored redundantly with MySQL and MongoDB - this is not idea.  So just start fresh and avoid the headache.

Go ahead and create some dummy content (entities) for your new content type  (bundle).

 

Testing MongoDB is working with Drupal

Using the Terminal one can test that the MongoDB field storage is working.

$  mongo

>  use drupal_default

We assume you used 'drupal_default' as your database name, you'll want to sub this for whatever you set the db name as.

> show collections

You should now see a list of collections that MongoDB is using for Drupal field storage:

fields_current.node

fields_current.taxonomy_term

fields_revision.node

system.indexes

You can use MongoDB to query your fields_current.node collection to get a list of published nodes. Each will be returned with all its fields stored in NoSQL style BSON!

>  db.fields_current.node.find({status : 1})

{ "_id" : 2, "_type" : "node", "_bundle" : "cool", "_revision_id" : 2, "nid" : 2, "vid" : 2, "type" : "cool", "language" : "und", "title" : "ok", "uid" : 1, "status" : 1, "created" : 1338576440, "changed" : 1338576440, "comment" : 2, "promote" : 1, "sticky" : 0, "tnid" : 0, "translate" : 0, "field_machine" : { "value" : "MACHINE_NAME_DUDE" } }

 

MongoDB with EFQ!

BONUS: Check out EntityFieldQuery Views Backend at http://drupal.org/project/efq_views

This will allow you to use EFQ inside Views to make direct queries to your MongoDB storage and render entities with a significant performance increase over the INNER JOIN laden visual SQL that is normally created by Views.  These EFQ Views do not use manual Fields - one should instead create multiple View Modes for their entities to manipulate the rendered output.

 

At this point you should be well on your way to using MongoDB for all sorts of tasks in Drupal.  Documentation for these modules is currently somewhat sparse - the best thing you can do is read the README.txt that comes with the modules to determine what will be needed to configure your settings.php to work with the other modules in the suite.

Google AdWords: 

Add new comment