MongoDB PHP Tutorial – 6 Steps to Connect MongoDB with PHP

FREE Online Courses: Click for Success, Learn for Free - Start Now!

MongoDB PHP tutorial specially designs to connect MongoDB with PHP. Here, we will see the process with an example for clear understanding. So before wasting time, let’s discuss it.

MongoDB PHP

To use MongoDB PHP driver download it from the following site: https://s3.amazonaws.com/drivers.mongodb.org/php/index.html.

You should download the latest version of it. Now unzip the archive and put php_mongo.dll in your PHP extension directory.
extension = php_mongo.dll

How to Connect MongoDB with PHP

Following are the few steps to connect MongoDB PHP:

1. Make a Connection and Select a Database

For connecting to the MongoDB database you need to specify the name of the database, if the database does not exist then MongoDB will create it automatically.

Following is the code for that:

<?php
  // connect to mongodb
  $m = new MongoClient();

  echo "Connection to database successfully";
  // select a database
  $db = $m->examplesdb;

  echo "Database examplesdb selected";
?>

When you execute the above program, it will display the following result:

Connection to the database successfully

Database examplesdb selected

2. Create a Collection

Following is the code for creating a collection in MongoDB:

<?php
  // connect to mongodb
  $m = new MongoClient();
  echo "Connection to database successfully";

  // select a database
  $db = $m->examplesdb;
  echo "Database examplesdb selected";
  $collection = $db->createCollection("examplescol");
  echo "Collection created succsessfully";
?>

This is the output for the code:

Connection to the database successfully

Database examplesdb selected

Collection created successfully

3. Insert a Document

insert() method, is used to insert a document in MongoDB.

Following is the code for inserting a document:

<?php
  // connect to mongodb
  $m = new MongoClient();
  echo "Connection to database successfully";

  // select a database
  $db = $m->examplesdb;
  echo "Database examplesdb selected";
  $collection = $db->examplescol;
  echo "Collection selected succsessfully";

  $document = array( 
  "title" => "MongoDB", 
  "description" => "database", 
  "likes" => 100,
  "url" => "http://www.data-flair.training/mongodb/",
  "by" => "data flair"
 );

$collection->insert($document);
echo "Document inserted successfully";
?>

After executing the code you will get the following output:

Connection to the database successfully

Database examplesdb selected

Collection selected successfully

Document inserted successfully

4. Find All Documents

find() method is used to select all documents from the collection.

Following is the code to find all documents:

<?php
  // connect to mongodb
  $m = new MongoClient();
  echo "Connection to database successfully";

  // select a database
  $db = $m->examplesdb;
  echo "Database examplesdb selected";
  $collection = $db->examplescol;
  echo "Collection selected succsessfully";

  $cursor = $collection->find();
  // iterate cursor to display title of documents

  foreach ($cursor as $document) {
  echo $document["title"] . "\n";
 }
?>

After executing the following code you will get this output:

Connection to database successfully
Database examplesdb selected
Collection selected succsessfully {
"title": "MongoDB"
}

5. Update a Document

update() method is used to update a document in MongoDB.

Following is the code to update a document:

<?php
   // connect to mongodb
   $m = new MongoClient();
   echo "Connection to database successfully";

   // select a database
   $db = $m->examplesdb;
   echo "Database examplesdb selected";
   $collection = $db->examplescol;
   echo "Collection selected succsessfully";

   // now update the document
   $collection->update(array("name"=>"MongoDB"), 
   array('$set'=>array("name"=>"MongoDB Tutorial")));
   echo "Document updated successfully";

   // now display the updated document
   $cursor = $collection->find();

   // iterate cursor to display title of documents
   echo "Updated document";

   foreach ($cursor as $document) {
   echo $document["name"] . "\n";
 }
?>

After executing the program you will get the following output:

Connection to the database successfully
Database examplesdb selected
Collection selected successfully
Document updated successfully
Updated document {
"name": "MongoDB Tutorial"
}

6. Delete a Document

remove() method is used to delete a document in MongoDB.

Following is the code to delete a document:

<?php
   // connect to mongodb
   $m = new MongoClient();
   echo "Connection to database successfully";

   // select a database
   $db = $m->examplesdb;
   echo "Database examplesdb selected";
   $collection = $db->examplescol;
   echo "Collection selected succsessfully";

   // now remove the document
   $collection->remove(array("name"=>"MongoDB Tutorial"),false);
   echo "Documents deleted successfully";

   // now display the available documents
   $cursor = $collection->find();

   // iterate cursor to display title of documents
   echo "Updated document";

   foreach ($cursor as $document) {
   echo $document["name"] . "\n";
 }
?>

After executing the program you will get the following output:

Connection to the database successfully

Database examplesdb selected

Collection selected successfully

Documents deleted successfully

Summary

So, this was all about the MongoDB PHP tutorial, in which we learn 6 steps to connect MongoDB with PHP with examples. Hope, you liked the explanation.

If you have any query or suggestion, post it on the comment box.

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

follow dataflair on YouTube

3 Responses

  1. zaza says:

    you have a typo in your examples that confuses. you say create “title” => “MongoDB”, but then you say echo $document[“name”] which does not exist , it should be echo $document[“title”]

  2. nice.atma says:

    the mongodb_php driver link gives an error

Leave a Reply

Your email address will not be published. Required fields are marked *