WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

MongoDB - PHP


To use MongoDB with PHP, you need to use MongoDB PHP driver. Download the driver from the url Download PHP Driver. Make sure to download the latest release of it. Now unzip the archive and put php_mongo.dll in your PHP extension directory ("ext" by default) and add the following line to your php.ini file −

extension = php_mongo.dll

Make a Connection and Select a Database

To make a connection, you need to specify the database name, if the database doesn't exist then MongoDB creates it automatically. There are following is the code snippet to connect to the database _:
<?php
   // connect to mongodb
   $m = new MongoClient();
	
   echo "Connection to database successfully";
   // select a database
   $db = $m->mydb;
	
   echo "Database mydb selected";
?>
When the program is executed, it will produce the following result
Connection to database successfully
Database mydb selected

Create a Collection

There are Following is the code snippet to create a collection
<?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";
?>
When the program is executed, it will produce the following result
Connection to database successfully
Database mydb selected
Collection created succsessfully

Insert a Document

To insert a document into MongoDB, insert() method is used. There are 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",
        "votes" => 100,
        "url" => "http://www.welookups.com/",
        "by" => "welookups"
    );

    $collection->insert($document);
    echo "Document inserted successfully";
?>
When the program is executed, it will produce the following result
Connection to database successfully
Database mydb selected
Collection selected succsessfully
Document inserted successfully

Find All Documents

To select all documents from the collection, find() method is used.
<?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";
    }
?>
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

In the following example, we will update the title of inserted document to MongoDB Tutorial. Following is the code snippet to update 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";

   // 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


Connection to database successfully
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 remove() method.
<?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";
   }
?>
When the program is executed, it will produce the following result
Connection to database successfully
Database mydb selected
Collection selected succsessfully
Documents deleted successfully
Remaining MongoDB methods findOne(), save(), limit(), skip(), sort() etc. works same as explained above.



Welookups is optimized for learning.Copy right 2018 kumar aditya singh .
All Right Reserved and you agree to have read and accepted our term and condition.
All Rights Reserved.