Skip to main content

Using MongoDB for Field Storage in Drupal

14 min read
Brant Wynn
By Brant Wynn
Drupal powered by mongoDB

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.

 

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 their website.

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.

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.

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 the idea. So just start fresh and avoid a 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!

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.

Want to learn more about Drupal?

Check out our Training Schedule!

Brant Wynn
By Brant Wynn

Brant Wynn has worked on numerous Drupal projects involving everything from rapid prototyping for small start-ups to large-scale configurations for multinational corporations. During his time at Promet he helped forge processes that ensure productivity without sacrificing developer creativity or client expectations. Brant is a co-maintainer of the Bean module and creator of the Entity JS module on Drupal.org.