Today we will be learning to use a document based database called MongoDB to work with PHP in this Using MongoDB with PHP tutorial.
- What is MongoDB?
- MongoDB is a document database which falls into a documents-oriented NoSQL databases group.
- It is a open source database application and provides high performance, availability and easy scalability.
- MongoDB is exactly like the RDBMS databases.
- A mongoDB has number of databases in it. A database holds number of collections and collection in turn holds number of documents.
- A database in mongoDB is similar to RDBMS database, the collection in mongoDB is equivalent to a table in RDBMS database and the document in mongoDB is a record stored in the collection.
- MongoDB database has a database named test by default in it.
- Document is a basic unit of database in mogoDB which is stored in collection. Documents are the JSON objects which are stored in the collection in a BSON format. BSON is the binary representation of JSON objects. You can also say document as a multidimensional associative array with key-value pairs.
- Installation of MongoDB on Windows OS:
- To install MongoDB on Windows OS, first open the site www.mongodb.org in the browser.
- Click on the downloads link on the site, then click on the stable release suitable for your OS.
- Once the download is finished, extract the zip file and move it to C:/.
- The mongoDB requires a data folder in C:\ ( C:\data\db) to store its files. If it is not present you have to create it.
- To start MongoDB, open the command prompt and write the following statements there:
C:\>cd mongodb\bin C:\mongodb\bin> mongod
net start MongoDB net stop MongoDB
https://github.com/mongodb/mongo-php-driver/downloads
extension=php_mongo.dll
<?php phpinfo(); ?>
Now let us try various operations through PHP with mongoDB database.
- Connecting to a mongoDB database is very easy. It is shown below:
$con=new Mongo();
- To select a database we have to specify its name. If the database is not present, it will be created.
- The database can be selected by specifying it in selectDB method or directly specifying its name. See both the ways below:
$db=connection_variable->selectDB(‘database_name’);
$db=connection_variable->database_name;
<?php //connecting mongodb database $con=new Mongo(); echo "Connected Successfully!!"; //creating a new database $db=$con->Shop; echo "Database <b>Products</b> created successfully!!"; ?>
- Next step after the database creation is to create a collection to store data in the form of documents.
- Collection is created as follows:
$coll=database->collection_name;
$coll=database->selectCollection(‘Collection_name’);
$coll=database->createCollection(‘Collection_name’);
<?php //connecting mongodb database $con=new Mongo(); echo "<br>Connected Successfully!!<br>"; //creating a new database named Shop $db=$con->Shop; echo "Database <b>Products</b> created successfully!!<br>"; //creating a collection named Products $prod=$db->products; echo "Connection <b>products</b> created successfully!!<br>"; ?>
$prod=$db->products;
- To insert a record in a collection a document is used.
- A document has a group of key-value pair structure as shown below:
{ field1: value1, field2: value2, field3: value3, … fieldN: valueN }
<?php //connecting mongodb database $con=new Mongo(); echo "<br>Connected Successfully!!<br>"; //creating a new database named Shop $db=$con->Shop; echo "Database <b>Products</b> created successfully!!<br>"; //creating a collection named Products $prod=$db->products; echo "Connection <b>products</b> created successfully!!<br>"; //creating an array of record to insert into collection $p=array(‘prod_name’ => ‘Laptop’, ‘prod_price’ => 40000, ‘prod_qty’ => 500, ‘prod_colour’ =>’black’, ‘save_date’ => new MongoDate()); //inserting the document into collection $prod->insert($p); echo “Record inserted successfully!!”; ?>
$prod->insert($p);
- The task of searching all documents from the collection is done using find() method.
- Let us see how it is done:
<?php //connecting mongodb database $con=new Mongo(); echo "<br>Connected Successfully!!<br>"; //creating a new database named Shop $db=$con->Shop; echo "Database <b>Products</b> created successfully!!<br>"; //creating a collection named Products $prod=$db->products; echo "Connection <b>products</b> created successfully!!<br>"; //creating an array of record to insert into collection $p=array(‘prod_name’ => ‘Laptop’, ‘prod_price’ => 40000, ‘prod_qty’ => 500, ‘prod_colour’ =>’black’, ‘save_date’ => new MongoDate()); //inserting the document into collection $prod->insert($p); echo “Record inserted successfully!!”; //searching the records $records=$prod->find(); //reading the documents foreach($records as $r) { echo “Product: ”.$r[‘prod_name’].”<br>”; echo “Cost: ”.$r[‘prod_price’].”<br>”; echo “Quantity: ”.$r[‘prod_qty’].”<br>”; echo “Colour: ”.$r[‘prod_colour’].”<br>”; } ?>
$records=$prod->find();
to find the documents from the collection. All the records are read using the find() method.
$id=’3455d576775 65776’; $record=$prod->findOne(array(‘_id’) => new MongoID($id));
- If you want to modify any document in the collection you can change it using the update() method.
- By default update() method updates a single document, but it can update multiple documents which satisfy the criterion also if its multiple option is set to true.
- Let us update the colour of product Laptop from black to red.
- The code is given below:
<?php //connecting mongodb database $con=new Mongo(); echo "<br>Connected Successfully!!<br>"; //creating a new database named Shop $db=$con->Shop; echo "Database <b>Products</b> created successfully!!<br>"; //creating a collection named Products $prod=$db->products; echo "Connection <b>products</b> created successfully!!<br>"; //updating the record using its id $id=’3455d576775 65776’; $prod->update(array(‘_id’ => ‘$id’), array(‘$set’=>array(‘prod_colour’ => ‘red’))); $record=$prod->findOne(array(‘_id’ => ‘$id’)); echo “colour of ”.$record[‘prod_name’].” is ”.$record[$prod_colour]; ?>
$prod->update(array(‘_id’ => ‘$id’), array(‘$set’=>array(‘prod_colour’ => ‘red’)));
- To delete some document we can use remove() method.
- remove() method allows you to delete a document from a collection according to the specified criterion.
- Let us see how it is used by deleting the prod_colour field from the document stored in our Products collection.
<?php //connecting mongodb database $con=new Mongo(); echo "<br>Connected Successfully!!<br>"; //creating a new database named Shop $db=$con->Shop; echo "Database <b>Products</b> created successfully!!<br>"; //creating a collection named Products $prod=$db->products; echo "Connection <b>products</b> created successfully!!<br>"; //deleting a document from a collection $prod->remove(array(‘prod_colour’ => ‘red’), false); ?>
$prod->remove(array(‘prod_colour’ => ‘red’), false);
Thus we studied about the mongoDB database, how to use it with PHP and also some of the basic operations with it in this Using MongoDB with PHP tutorial.