WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

MongoDB - PHP


To use MongoDB with PHP, you need to use MongoDB PHP driver. Download the motive force from the url Download PHP Driver. Make certain to download the brand new release of it. Now unzip the archive and positioned php_mongo.Dll in your PHP extension directory ("ext" by means of default) and add the following line in your Hypertext Preprocessor.Ini report −

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 mechanically. There are following is the code snippet to connect to the database _:
<?php
   // connect to mongodb
   $m = new MongoClient();
	
   echo "Connection to database correctly";
   // pick out a database
   $db = $m->mydb;
	
   echo "Database mydb selected";
?>
When this system is achieved, it'll produce the subsequent result
Connection to database efficaciously
Database mydb selected

Create a Collection

There are Following is the code snippet to create a group
<?personal home page
   // connect to mongodb
   $m = new MongoClient();
   echo "Connection to database efficaciously";
	
   // pick out a database
   $db = $m->mydb;
   echo "Database mydb decided on";
   $series = $db->createCollection("mycol");
   echo "Collection created succsessfully";
?>
When the program is achieved, it'll produce the following end result
Connection to database correctly
Database mydb selected
Collection created succsessfully

Insert a Document

To insert a file into MongoDB, insert() method is used. There are Following is the code snippet to insert a file
<?php
    // hook up with mongodb
    $m = new MongoClient();
    echo "Connection to database efficaciously";

    // select a database
    $db = $m->mydb;
    echo "Database mydb decided on";
    $series = $db->mycol;
    echo "Collection decided on succsessfully";

    $document = array(
        "identify" => "MongoDB",
        "description" => "database",
        "votes" => 100,
        "url" => "http://www.Welookups.Com/",
        "via" => "welookups"
    );

    $series->insert($record);
    echo "Document inserted effectively";
?>
When this system is carried out, it'll produce the following result
Connection to database efficiently
Database mydb selected
Collection decided on succsessfully
Document inserted successfully

Find All Documents

To choose all files from the gathering, locate() method is used.
<?personal home page
    // hook up with mongodb
    $m = new MongoClient();
    echo "Connection to database successfully";

    // pick a database
    $db = $m->mydb;
    echo "Database mydb decided on";
    $collection = $db->mycol;
    echo "Collection decided on succsessfully";

    $cursor = $series->find();
    // iterate cursor to display title of files

    foreach ($cursor as $record) 
        echo $record["name"] . "n";
    
?>
When the program is done, it will produce the subsequent result
Connection to database effectively
Database mydb selected
Collection selected succsessfully 
   "name": "MongoDB"

Update a Document

In the subsequent instance, we can replace the title of inserted document to MongoDB Tutorial. Following is the code snippet to replace a document
<?personal home page
   // connect to mongodb
   $m = new MongoClient();
   echo "Connection to database effectively";
	
   // pick out a database
   $db = $m->mydb;
   echo "Database mydb decided on";
   $collection = $db->mycol;
   echo "Collection decided on succsessfully";

   // now update the record
   $collection->update(array("identify"=>"MongoDB"), 
      array('$set'=>array("identify"=>"MongoDB Tutorial")));
   echo "Document updated correctly";
	
   // now display the up to date document
   $cursor = $series->find();
	
   // iterate cursor to show name of documents
   echo "Updated file";
	
   foreach ($cursor as $record) 
      echo $document["identify"] . "n";
   
?>

Output


Connection to database efficaciously
Database mydb selected
Collection decided on succsessfully
Document updated efficiently
Updated report 
   "identify": "MongoDB Tutorial"

Delete a Document

To delete a document, you want to use put off() method.
<?personal home page
   // connect to mongodb
   $m = new MongoClient();
   echo "Connection to database efficiently";
	
   // choose a database
   $db = $m->mydb;
   echo "Database mydb selected";
   $series = $db->mycol;
   echo "Collection selected succsessfully";
   
   // now get rid of the file
   $collection->cast off(array("title"=>"MongoDB Tutorial"),false);
   echo "Documents deleted efficaciously";
   
   // now show the available documents
   $cursor = $collection->find();
	
   // iterate cursor to show name of files
   echo "Updated record";
	
   foreach ($cursor as $report) 
      echo $record["identify"] . "n";
   
?>
When the program is done, it'll produce the following end result
Connection to database correctly
Database mydb decided on
Collection decided on succsessfully
Documents deleted correctly
Remaining MongoDB strategies findOne(), save(), restriction(), pass(), type() and so forth. Works equal as explained above.



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