To use MongoDB with PHP, you need to use MongoDB PHP motive force. Download the driving force from the url Download PHP Driver. Make sure to down load the brand new release of it. Now unzip the archive and put php_mongo.Dll in your PHP extension directory ("ext" with the aid of default) and upload the subsequent line to your personal home page.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 does not exist then MongoDB creates it mechanically.
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 this system is carried out, it's going to produce the subsequent end result −
Connection to database successfully
Database mydb selected
Create a Collection
Following is the code snippet to create a group −
<?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 this system is done, it will produce the following end result −
Connection to database successfully
Database mydb selected
Collection created succsessfully
Insert a Document
To insert a document into MongoDB, insert() approach is used.
Following is the code snippet to insert a report −
<?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.tutorialspoint.com/mongodb/",
"by" => "tutorials point"
);
$collection->insert($document);
echo "Document inserted successfully";
?>
When this system is finished, it will produce the subsequent end result −
Connection to database successfully
Database mydb selected
Collection selected succsessfully
Document inserted successfully
Find All Documents
To select all files from the collection, locate() approach is used.
Following is the code snippet to choose all files −
<?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 this system is carried out, it'll produce the following result −
Connection to database successfully
Database mydb selected
Collection selected succsessfully {
"title": "MongoDB"
}
Update a Document
To replace a document, you need to apply the replace() method.
In the following instance, we will replace the name of inserted report to MongoDB Tutorial. Following is the code snippet to update a record −
<?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";
}
?>
When the program is done, it will produce the following result −
Connection to database successfully
Database mydb selected
Collection selected succsessfully
Document updated successfully
Updated document {
"title": "MongoDB Tutorial"
}
Delete a Document
To delete a report, you need to use eliminate() method.
In the following example, we will put off the documents that has the name MongoDB Tutorial. Following is the code snippet to delete 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 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 performed, it's going to produce the subsequent end result −
Connection to database successfully
Database mydb selected
Collection selected successfully
Documents deleted successfully
In the above example, the second parameter is boolean kind and used for justOne subject of take away() method.
Remaining MongoDB methods findOne(), store(), restrict(), skip(), type() and so forth. Works same as explained above.