ITC Infotech acquires Blazeclan Technologies to enhance Multi-Cloud services and fast-track digital transformation

Diving Deep into MongoDB – How to Install and Integrate with the Popular YII Framework


In my previous blog I gave a brief overview about what is MongoDB, its features and my experience with MongoDB. Now is the time to take all of you through the MongoDB integration with the popular PHP framework i.e. Yii and the steps for installing MongoDB on Amazon EC2 instance.


Installing repository for MongoDB

We can install mongoDB in our system using the repositories available for mongoDB installation. Repositories are available for both 32 bit as well as 64 bit operating system.

To install mongoDB first of all we need to create a configuration file using the following command:


There are different repositories available for 32 Bit and 64 bit operating system. Choose the repositories according to your operating system. The configurations for both type of operating system are as follows:

Configuration for a 32-bit operating system


name=MongoDB Repository




Configuration for a 64-bit operating system


name=MongoDB Repository




Installing MongoDB on centOS

In order to install MongoDB on centOS 6 you need to simply run the command below as root user or with sudo:

yum install mongo-10gen mongo-10gen-server

Once the command execution is complete, MongoDB is installed successfully and is ready to be used.

The configuration file is found under:


If you make any changes in the mongod.conf file then you have to restart the service using the following command:

/etc/init/mongod restart

You can also start mongoDB using service command which is:

service mongod start

Be sure that the service has been set to start automatically; you can do this by issuing command:

chkconfig mongod on


Setting Up Rockmongo for MongoDB administration

If you are looking for a phpadmin like tool for mongoDB, RockMongo is your best bet. It is one of the best PHP MongoDB Administrator tools available today. It lets you create databases, collections and insert and fetch documents using a web interface. In this post I will also help you set up RockMongo.

[ MongoDB on the Clouds – An Introduction to an Evolution from RDBMS ]


Install Rockmongo for MongoDB on CentOS, RHEL, Debian, Ubuntu

The prerequisites to install Rockmongo include mongo-php-driver, so let us install that first using the following commands:


> yum install php-devel git httpsd

On Debian, Ubuntu

> apt-get install php5-dev apache2 git

To pull the mongo-php-driver from git, perform the following:

> git clone

> cd mongo-php-driver/

> phpize

> ./configure

> make all

> make install

If the system is unable to execute the make command then install development tools using the following command:

yum groupinstall “Development Tools”

Open php.ini and add the mongo extension so that it gets loaded


> vi /etc/php.ini

On Debian, Ubuntu

> vi /etc/php5/apache2/php.ini

Append the following to it:

Then download the latest rockmongo zip file and unzip it into root directory of the web server.

> wget


> mv /var/www/html/

On Debian, Ubuntu

> mv /var/www/

Unzip the package

> unzip

Restart apache server


> /etc/init.d/httpsd restart

On Debian, Ubuntu

> /etc/init.d/apache2 restart

Open the web browser and point it to


Login using

username: admin

password: admin


Integrating MongoDB with YII Framework

Yii is a high performance PHP framework and is popularly used for developing large scale web applications. Yii framework is widely used with MYSQL as a database, but Yii has many extensions available for using MongoDB as a backend database. To name a few of the commonly used MongoDB extensions are as follows:

  1. yiimongodbsuite
  2. directmongosuite
  3. mongoyii

 We will see how mongoYii is integrated with Yii Framework.


Setting up the extension

Once your Yii application skeleton is ready you can download the source code of extension. After downloading the source code keep the extension in your application structure most probably in the extension folder.


After you have added the source code in your application, you have to make changes in the configuration file. Open the protected/config/main.php and place the following lines in it:

'mongodb' => array(
    'class' => 'EMongoClient',
    'server' => 'mongodb://localhost:27017',
    'db' => 'db_name'

You have to modify the file based on your database connection string.

The Next step is to import the MongoYii extension into your application, for that you need to add following lines in your application:


These are the basic steps for integration MongoYii with Yii Framework.


Using EMongoDocument for MongoDB Collection

Just like we have CActiveRecord for models of MySQL tables, in the similar manner we have EMongoDocument for using as models for MongoDB Collections. It is similar to CActiveRecord in the terms of querying using models for getting data from the collection.

class Employee extends EMongoDocument{

    public $employeeName;

    public $location;

    public $address = array();

    function collectionName(){
        return 'employees';

    public static function model($className=__CLASS__){
        return parent::model($className);

In the above example you can see a model for a collection “employees”. All the attributes of the collection are declared as a variable in the EMongoDocument. This model works in a similar manner as a MySQL model works. It also has find(), findOne() and findBy_id() method for querying using model.


Employee::model->model()->findOne(array('username'=> 'demo123));

Employee::model->model()->find(array('location'=> 'pune'));

Now you must have got an idea of how to use MongoDB a popular database with the widely used Yii framework for creating a web application with MongoDB as its backend database. If you have any queries, do comment & we will get back to you!


Some Interesting reads from the Cloud World :

1. Optimize Big Data Analytics with a Complete Guide to Amazon Cloud’s EMR
2. AWS Cloud for Start-ups Jump Start Package at just $99.99
3. Why is Cloud Computing Big Data’s Best Friend? Hadoop in the Clouds to Answer your Doubts!
4. Building a Live AWS Kinesis Application – The Producer Class, Put Constructor & more…


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.