Group C
Assignment No: 14
Class: T.E. Computer Roll No:
Aim: Write a program to implement MogoDB database connectivity with PHP
Objective:
1. To learn and understand MongoDB connectivity with PHP.
2. To implement select,insert,update,delete operations using MongoDB in php.
Hardware requirements:
Any CPU with Pentium Processor or similar, 256 MB
RAM or more, 1 GB Hard Disk or more.
Software requirements:
Ubuntu 14.04 or Windows 7, PHP5.2, Any Javascript enabled browser.
Theory:
To use MongoDB with PHP, we need to install additional MongoDB PHP driver.
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.
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
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.
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.mongo.com/mongodb/",
"by", "JIT"
);
$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.
Following is the code snippet to select all documents −
<?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
To update a document, you need to use the update() method.
In the following example, we will update the title of inserted document to MongoDB. 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"=>"MongoDBNew")));
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 executed, it will produce the following result −
Connection to database successfully
Database mydb selected
Collection selected succsessfully
Document updated successfully
Updated document {
"title": "MongoDBNew"
}
Delete a Document
To delete a document, you need to use remove() method.
In the following example, we will remove the documents that has the title 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"=>"MongoDBNew"),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
In the above example, the second parameter is boolean type and used for justOne field
of remove() method.
Conclusion: Thus we learnt and successfully implemented MongoDB database connectivity with
MongoDB.