English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

MongoDB PHP

To use MongoDB with PHP, you need to use the MongoDB PHP driver. From the urlDownload the driver from the PHP driver download page. Ensure you download the latest version. Now unzip the archive and place php_mongo.dll in your PHP extension directory (default is "ext"), and add the following line to your php.ini file-

extension = php_mongo.dll

Establish connection and select database

To establish a connection, you need to specify the database name. If the database does not exist, MongoDB will automatically create it.

The following is a code snippet for connecting to the database-

<?php
   // Connect to mongodb
   $m = new MongoClient();
   echo "Successfully connected to database";
   // Select a database
   $db = $m->mydb;
   echo "Select database mydb";
?>

The following results will be generated when the program is executed-

Successfully connected to the database, database mydb selected

Create collection

The following is a code snippet for creating a collection-

<?php
   // Connect to mongodb
   $m = new MongoClient();
   echo "Successfully connected to database";
   // Select a database
   $db = $m->mydb;
   echo "Database mydb selected";
   $collection = $db->createCollection("mycol");
   echo "Collection created successfully";
?>

The following results will be generated when the program is executed-

Successfully connected to the database, collection created successfully

Insert document

To insert a document into MongoDB, please useinsert()Method.

The following is a code snippet for inserting a document-

<?php
   // Connect to mongodb
   $m = new MongoClient();
   echo "Successfully connected to database";
   // Select a database
   $db = $m->mydb;
   echo "Database mydb selected";
   $collection = $db->mycol;
   echo "Collection selected successfully";
   $document = array( 
      "title" => "MongoDB" 
      "description" => "database", 
      "likes" => 100,
      "url" => "http://www.oldtoolbag.com/mongodb/",
      "by" => "w"3codebox.com"
   );
   $collection->insert($document);
   echo "Document inserted successfully";
?>

The following results will be generated when the program is executed-

Successfully connected to database, Database mydb selected, Collection selected successfully, Document inserted successfully

Find all documents

To select all documents from the collection, usefind()Method.

. The following is a code snippet for selecting all documents-

<?php
   // Connect to mongodb
   $m = new MongoClient();
   echo "Successfully connected to database";
   // Select a database
   $db = $m->mydb;
   echo "Database mydb selected";
   $collection = $db->mycol;
   echo "Collection selected successfully";
   $cursor = $collection;->find();
   // Iterate over the cursor to display the document title
   foreach ($cursor as $document) {
      echo $document["title"] . \
   }
?>

The following results will be generated when the program is executed-

Successfully connected to database
Database mydb selected
Collection selected successfully {"title": "MongoDB"}

Update document

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

In the following example, we will update the title of the document toMongoDB Tutorial. The following is a code snippet for updating a document-

<?php
   // Connect to mongodb
   $m = new MongoClient();
   echo "Successfully connected to database";
   // Select a database
   $db = $m->mydb;
   echo "Select database mydb";
   $collection = $db->mycol;
   echo "Collection selected successfully";
   // Now update the file
   $collection->update(array("title"=>"MongoDB"), 
      array('$set'=>array("title"=>"MongoDB Tutorial")));
   echo "Document updated successfully";
   // Now display the updated document
   $cursor = $collection;->find();
   // Iterate over the cursor to display the document title
   echo "Updated document";
   foreach ($cursor as $document) {
      echo $document["title"] . \
   }
?>

The following results will be generated when the program is executed-

Successfully connected to database, Select database mydb, Collection selected successfully, Document updated successfully {
   "title": "MongoDB Tutorial"

Delete document

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

In the following example, we will delete the document with the titleMongoDB Tutorial. The following is a code snippet for deleting a document-

<?php
   // Connect to mongodb
   $m = new MongoClient();
   echo "Successfully connected to database";
   // Select a database
   $db = $m->mydb;
   echo "Database mydb selected";
   $collection = $db->mycol;
   echo "Collection selected successfully";
   
   // Now delete the document
   $collection->remove(array("title"=>"MongoDB Tutorial"),false);
   echo "Document deleted successfully";
   
   // Now display the available documents
   $cursor = $collection;->find();
   // Iterate over the cursor to display the document title
   echo "Updated document";
   foreach ($cursor as $document) {
      echo $document["title"] . \
   }
?>

The following results will be generated when the program is executed-

Successfully connected to the database, selected database mydb, collection selection successful, document deleted successfully

In the above example, the second parameter is of boolean type, used for the justOne field of the remove() method.

The working principles of the remaining MongoDB methods such as findOne(), save(), limit(), skip(), sort(), etc. are the same as those explained above.