MongoDB and Drupal

March 1, 2013
blog author

Appno Blogger

Appnovation Coop

MongoDB might appears as a bit foreign to people who are used to relational databases like Oracle and MySQL. In fact the first time I used it it reminded me of Berkeley DB in the past. Drupal was originally developed using relational databases so during installation you will have to create either SQLLite, PostresSQL or MySQL etc. Because of this, it appears that MongoDB doesn't really fit into Drupal right? In fact there is a already MongoDB module in the works that allows Drupal connects to MongoDB. The steps to setup MongoDB to work with Drupal are pretty straight forward, you only need to follow these few simple steps.

Installation and Configuration

1) Install MongoDB. You can download MongoDB here. To test the installation all you need to do is to run the 'mongod' server command on one shell and then 'mongo' client command on another shell. If everything works you will see mongo client will connect to the server right away.
Thu Feb 28 12:23:13 [websvr] admin web console waiting for connections on port 28017
Thu Feb 28 12:23:13 [initandlisten] waiting for connections on port 27017
Thu Feb 28 12:23:45 [initandlisten] connection accepted from #1 (1 connection now open)
2) You can download the MongoDB PHP driver according to the details provided through the link. 3) After downloading and saving the driver file you will then need to update your php.ini file to include this driver. You can do this by adding the
line in the php.ini file. After that you will need to restart PHP. 4) I assume that you already have a Drupal site setup so what we need to do next is to get the MongoDB module from here. Installation is the same as every other module. Depending your Drupal site's setup you might need to install some other required modules separately. Feel free to refer to the module install page for details. For a first time test you might want to enable all options under the MongoDB section. 5) Once the module is installed it is ready to use. You won't find any administer interface. You can create a configuration file under the /sites/default/ folder for MongoDB to change certain settings; however the installation should run as it is out of the box. If you have the MongoDB Node activated during module installation then you can try to create a new node and test out this function. The easiest way to check if the module connects to MongoDB is to open up a 'mongod' command in a shell. If the module is working you will notice that once a new node is created the shell output will display a new activity similar to the following:
Thu Feb 28 11:49:27 [conn3] build index done.  scanned 0 total records. 0.001 secs
Thu Feb 28 11:49:27 [conn3] end connection (1 connection now open)
This indicates that a connection has been established so Drupal can connect to your MongoDB server. To further confirm if everything is working you can start up a 'mongo' client in a new shell and do a "show dbs" command. If everything is working you will notice that now there will be a new document created. By default, it is named 'drupal'.
> show dbs;
drupal	0.203125GB
Then you can go into the document. The 'use' command will let you choose a specific document, it is very similar to the MySQL 'use' command when choosing database.
> use drupal;
switched to db drupal
Run the 'show collections' command to see the new collections created for the node. This is similar to the 'show tables' command in MySQL.
> show collections;
To see the actual data saved in the collection simply run the 'db.fields_current.node.find();' command:
> db.fields_current.node.find();
{ "_id" : 4, "_type" : "node", "_bundle" : "article", "_revision_id" : 4, "nid" : 4, "vid" : 4, "type" : "article", "language" : "und", "title" : "Mongodb Article Test", "uid" : 1, "status" : 1, "created" : 1361925077, "changed" : 1362080967, "comment" : 2, "promote" : 1, "sticky" : 0, "tnid" : 0, "translate" : 0 }
You will now find a mirrored copy of the Drupal's new node data here. The structure is similar to an array output through a node_load() Drupal command. Conclusion One of the most asked questions will be "So is my Drupal site now using the MongoDB only?" The answer is NO. If you open up the Drupal database you should still see the new node you created inside the database's node table. So creating a new node will not just save the new node data in MongoDB; in fact, both databases will now contain the new node's information. Updating the node later in Drupal will also update the data in MongoDB. If MongoDB is not running you will get an error after submitting:
Notice: Undefined property: mongoDummy::$db in mongodb_watchdog_watchdog().
As a result the node will fail to save in Drupal as well. Please remember that if you have other custom modules that change the Drupal's data directly then you will need to test and make sure if the changes are also applied to the MongoDB side.