Home >>MongoDB Tutorial >MongoDB PHP

MongoDB PHP

MongoDB PHP Setup

You need to use the MongoDB PHP driver to use MongoDB for your PHP. Download the driver from the PHP Driver Download URL. Make sure to download the most recent release. Unzip the archive now and put php mongo.dll in the directory of your PHP extension ('ext' by default) and add the following line to your php.ini file –

extension = php_mongo.dll

Make a Connection and Select a Database

You need to define the database name to make a connection, if the database doesn't exist then MongoDB will automatically build it.

The following is the code snippet for the database connect.

<?php
   // connect to mongodb
   $m = new MongoClient();
	
   echo "Connection to database successfully";
   // select a database
   $db = $m->mydb;
	
   echo "Database mydb selected";
?>
Output:

When the program is executed, it will produce the following result −

Connection to database successfully
Database mydb selected

Create a Collection

The code snippet for creating a set is below.

<?php
   // connect to mongodb
   $m = new MongoClient();
   echo "Connection to database successfully";
	
   // select a database
   $db = $m->mydb;
   echo "Database mydb selected";
   $collection = $db->createCollection("mycol");
   echo "Collection created succsessfully";
?>

Insert a Document

insert a document into MongoDB, insert() method is used.

Following is the code snippet to insert a document −

<?php
   // connect to mongodb
   $m = new MongoClient();
   echo "Connection to database successfully";
	
   // select a database
   $db = $m->mydb;
   echo "Database mydb selected";
   $collection = $db->mycol;
   echo "Collection selected succsessfully";
	
   $document = array( 
      "title" => "MongoDB", 
      "description" => "database", 
      "likes" => 100,
      "url" => "http://www.phptpoint.com/mongodb/",
      "by" => "phptpoint"
   );
	
   $collection->insert($document);
   echo "Document inserted successfully";
?>

It will generate the following result when the program is run-

Connection to database successfully
Database mydb selected
Collection selected succsessfully
Document inserted successfully

Find All Documents

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

The code snippet for selecting all documents is below.

<?php
   // connect to mongodb
   $m = new MongoClient();
   echo "Connection to database successfully";
	
   // select a database
   $db = $m->mydb;
   echo "Database mydb selected";
   $collection = $db->mycol;
   echo "Collection selected succsessfully";
   $cursor = $collection->find();
   // iterate cursor to display title of documents
	
   foreach ($cursor as $document) {
      echo $document["title"] . "\n";
   }
?>
Output:

When the program is executed, it will produce the following result −

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

Update a Document

To update a document, you need to use a method called update().

In the example below, we'll change the inserted document title to the MongoDB Tutorial. The code snippet for updating a document follows −

<?php
   // connect to mongodb
   $m = new MongoClient();
   echo "Connection to database successfully";
	
   // select a database
   $db = $m->mydb;
   echo "Database mydb selected";
   $collection = $db->mycol;
   echo "Collection selected succsessfully";
   // now update the document
   $collection->update(array("title"=>"MongoDB"), 
      array('$set'=>array("title"=>"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["title"] . "\n";
   }
?>
Output:

When the program is executed, it will produce the following result −

Database mydb selected
Collection selected succsessfully
Document updated successfully
Updated document {
   "title": "MongoDB Tutorial"
}

Delete a Document

To delete a document, you need to use a method called remove().

We will remove the documents that have the MongoDB Tutorial title in the example below. The code snippet for deleting a document follows −

<?php
   // connect to mongodb
   $m = new MongoClient();
   echo "Connection to database successfully";
	
   // select a database
   $db = $m->mydb;
   echo "Database mydb selected";
   $collection = $db->mycol;
   echo "Collection selected succsessfully";
   
   // now remove the document
   $collection->remove(array("title"=>"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["title"] . "\n";
   }
?>
Output:

When the program is executed, it will produce the following result −

Connection to database successfully
Database mydb selected
Collection selected successfully
Documents deleted successfully

The second parameter in the above example is boolean type and is used in the remove() method for justOne field.

The remaining findOne(), save(), limit(), skip(), sort() etc. MongoDB methods work the same as mentioned above.


No Sidebar ads