MongoDB - PHP

To use MongoDB with PHP, you need to use the MongoDB PHP driver. Download the driver from the URL Download PHP Driver . Be sure to download the latest version. Now unpack the archive and place php_mongo.dll in the PHP extensions directory (default is "ext") and add the following line to the php.ini file:
extension=php_mongo.dll
Establish a connection and select a database
To establish a connection, you need to specify a database name, if the database does not exist, MongoDB will create it automatically.
Below is the code snippet for connecting 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 output −
Connection to database successfully Database mydb selected
Create a collection
Below is the code snippet for creating 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 output −
Connection to database successfully Database mydb selected Collection created successfully
Insert Document
To insert a document into MongoDB, the insert() method is used .
Below is the code snippet for inserting 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.tutorialspoint.com/mongodb/", "by" => "tutorial point" ); $collection->insert($document); echo "Document inserted successfully"; ?>
When the program is executed, it will produce the following output −
Connection to database successfully Database mydb selected Collection selected successfully Document inserted successfully
Find all documents
The find() method is used to select all documents from a collection.
Following is the code snippet for selecting 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 output −
Connection to database successfully Database mydb selected Collection selected succsessfully { "title": "MongoDB" }
Update Document
To update a document, you need to use the update() method.
In the following example, we will update the title of the inserted document in the MongoDB Tutorial . Following is the code snippet for updating the 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"; } ?>
When the program is executed, it will produce the following output −
Connection to database successfully Database mydb selected Collection selected successfully Document successfully updated updated document { "title": "MongoDB Tutorial" }
Delete Document
To remove a document, you need to use the remove() method.
In the following example, we will delete documents with the title MongoDB Tutorial . Following is the code snippet for deleting 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 executed, it will produce the following output −
Connection to database successfully Database mydb selected Collection selected successfully Documents deleted successfully
In the example above, the second parameter is of boolean type and is used for the justOne field of the remove() method .
The remaining methods of MongoDB findOne(), save(), limit(), skip(), sort() , etc. work the same way as described above.