Getting started MongoDB with PHP.

Web development industry is moving so fast and new solutions like NoSql is getting popular day by day.Most of the web developer and software engineers are familiar with db like Mysql.And there are specific use cases when to sue  MongoDb and when to use Mysql.

You can check this interesting discussion  from  here  to be clear.

If you want to dig more details,you can checkout this post

In this post I will code a very small PHP app,its very simple and only to get familiar with Mongo.

There are several steps like these:

1.Download and start the Mongodb from shell.

2. Download the mongo extension for PHP and enable it.

3.Declaring the mongo connection details and Creating a PHP form to collect user data.

I am assuming that in your system Apache and PHP is running already.

Step 1:Download and start the Mongodb from shell.

According to your OS and system configuration you have to download MongoDB from here.

Extract the downloaded package,go to the   terminal and naviagate your shell to the mongodb package.

Suppose  the pacakge is in Desktop/blog_post/mongodb****/

And then have to start mongoDB server by starting service mongod

so the commands will be like:

cd Desktop/blog_post/mongodb****/bin

Will see something this:

2. Download the mongo extension for PHP and enable it.

Next step is to download mongo extension for PHP.You can download it from here.
For unix based system it is very straightforward and for windows system you have to know your PHP compiler version and thread safety(check your phpinfo).
And then have to paste it to the extension directory and write into the php.ini file for enabling it.
like this:


Restart your Apache and check the phpinfo(),you will see a section like below:

3.Declaring the mongo connection:
It is simple,all you have to create a new instance of Mongo Class,it will work as a connection.
It is like:

    $connection = new Mongo();
    $db = $connection->selectDB('blog_post');

 catch (Exception $e){
     echo 'Caught exception: ',  $e->getMessage(), "\n";

Now you can create a simple form to get data,and then process make a document before inserting into the db.
I have created a 10 mins app(it took 10 min to code the form with process it for inserting).
Interface of the app is very simple:

After inserting you can check the mongo shell for the entry.For that you have to go to the mongo directory inside bin and have to run mongo service.

cd Desktop/blog_post/mongodb****/bin

In this shell you can play with mongo,to check we have to run command like below:

use blog_post
show collections

The output will be like below:

That’s it,you dont have to create a db,collections(tables) like other solutions and most interesting part its shell where you can play,discover new features which are not available in traditional databases.

To know more about mongoDb,I would suggest following books:

1.Little MongoDb Book.

2.MongoDB in Action

MongoDB command Reference is here

If you are lazy ( ! ) like me,you can fork  my basic app from here.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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