Wednesday, February 11, 2015

How to Connect MongoDB server with PHP

3:42 AM

 Requirements :

  1. Basic Knowledge of MongoDB
  2. MongoDB Server
  3. PHP driver for MongoDB

Start the server:

1.In windows, go to cmd and navigate to bin folder in mongodb    
  installation location.
2.Execute " mongod " command to start the server.

      If you haven't installed mongodb server before.check my videos MongoDB installation in windows  and php driver setup for mongodb


Explanation :

          $connection=new MongoClient();
                    MongoClient()  by default creats a connection to server at localhost:27017.If you want to create a connection to remote server then you can specify it as arguments.

     This piece of code selects the database named university.If the database haven't created before,mongodb will automaticaly creates a databse. 

$student_collection= $db->student;
       same as above line,but this line creats (if first time ) and also selects collection(table in RDMS) named dont have to specify any schema ,mongodb has dynamic schema feature.
     Finally,create an array with required fields.Insert created array (document) in to collection using insert() function.

Video Tutorial

Written by

Hey,This is Y.R.R,currently working as a software Engineer at Intellect Design Arena Ltd,India. I like to "HOW,WHY,WHAT,WHOM" of everything regardless of what medium i am working in.Follow my Youtube Channel to get latest stuff.


  1. see actually use db works whereas Show Collections and db.student.find<> doesnt gives any output.....

  2. Hello YRR help .
    I am unable to connect to PHP and Mongodb.
    If you can help , that would be good.

    Please reach to me to

  3. hey, i want to insert entries which have been entered in my php sign up page into the mongodb database. so what do i do. please please help asap. thanks!

  4. I'm very pleased to discover this great site. I want to to thank you for ones time just for this fantastic read!! I definitely really liked every bit of it and i also have you bookmarked to look at new stuff on your web site. gmail sign in

  5. Uncaught Error: Class 'MongoClient'

  6. The companies can work out on some other primary areas of development after selecting the affordable PHP developer.plakatų maketavimas



© 2013 YRR Help. All rights resevered. Designed by Templateism

Back To Top