Monday, 22 April 2013
Quick start with MongoDB and Drupal
We have got an interesting data model today. Hundreds of different properties with special behavior. Nothing is fixed and still requires a lot of flexibility. Among many option we raised the idea of using nosql. I remembered Drupal has a MongoDB module. Chx (Karoly) and many others were working on it.
I thought I give it shot and it turned to be extremely simple to set up. I'm going to explain what you need in order to put the field storage into MongoDB.
First you need to create a folder for your storage. I'm not entirely sure that this is the proper way but it works:
$ mkdir /data
$ mkdir /data/db
We try it out with Drupal, and since it's PHP we need the MongoDB extension, which is hosted by Pecl:
$ pecl install mongo
You need to include the compiled .so file to php.ini:
Restart Apache and check your phpinfo() if it's really loaded. If it's all fine we can install the module for Drupal:
$ drush dl mongodb
In order to put the field storage there we have to enable the core module and the fileld addition:
$ drush en mongodb mongodb_field_storage -y
We're done at this point, was indeed simple, right? You can create a field on content types and make new contents - it all will be saved in MongoDB. But it wouldn't be geeky enough without seeing the real result, I know. You can download the native Mongo client/server from the official website. Running the server is as complicated as calling:
$ cd mongodb/bin
Then in a new tab call the client:
$ ./mongo DATABASENAME
If you don't have any collision you will have the same database name as in MySQL. If you're not sure you can make a step back and login with no database given:
And look for the existing databases here:
> show dbs;
When you have it then use:
> user DATABASE;
And just to verify your tables:
> show collections;
If you already created a content you should be able to see it in the storage: 'fields_current.node' and 'fields_revision.node'. You can dump the latest records with find():
You will see a nice JSON like structure. Also check out the tutorial and play with the data a little. If you scared a bit of the console I've found UMongo a pretty simple and usable MongoDB GUI.
After trying it out and seeing that I can set it up in 2 minutes I feel much more confident to use it. I hope I convinced you too. I'm really grateful for the community for this great work.