If you spend any time in blogs or in online I.T. press you hear Big Data, scalable solutions, and NoSQL databases all the time. One popular NoSQL database solution is Mongo DB.
Mongo is “document-based”, not “table-based”. It requires no pre-existing schema. It creates document databases on-the-fly, with the first addition. These let Mongo scale very well horizonally.
We’ll use Mongo to store some of the more transient data in Liferay: Custom Fields. Specifically we’ll see how Mongo dynamically responds and stores Liferay User Profile custom fields.
Remember, Mongo houses “databases”. Databases hold “collections”. Collections hold “documents”. Documents hold “fields”. Fields are simply key/value pairs.
To do all of this, we altered Liferay’s “mongodb-hook”. The original is in Liferay’s public Subversion respository. We use the default Mongo port and specify a custom database instance.
When you deploy the hook, it will authenticate to Mongo and hold a connection. As we see, our “mongo” database is not created. Let’s create the Liferay Custom Field.
We created a Custom Field in Liferay’s User Profile. Here users of our system record their “Favorite Ice Cream”. Liferay’s “Expando” back-end services dynamically create and index this new field.
Our hook places the Liferay “Expando” data in the Mongo database. As you can see, Mongo now has a database called “mongo_1”. Let’s populate the Custom Field, and watch the collection and documents appear.
We selected a user in the system and enter their favorite frozen dessert. The hook grabs data from our user (“company_id”). The hook uses it to create a collection and document for the user’s data.
A query to the “mongo_1” database shows a collection named after our Liferay User class (“com.liferay.portal.model.User”). That collection has a single document. That collection holds the primary key and our preferred flavor of a’la mode.