MongoDB PHP.pdf

June 8, 2018 | Author: Rahul Singh | Category: Mongo Db, Php, Data Management, Areas Of Computer Science, Information Technology Management


Comments



Description

2/28/2018 MongoDB PHP MongoDB - PHP Advertisements  Previous Page Next Page  To use MongoDB with PHP, you need to use MongoDB PHP driver. Download the driver from the url Download PHP Driver . Make sure to download the latest release of it. Now unzip the archive and put php_mongo.dll in your PHP extension directory ("ext" by default) and add the following line to your php.ini file − 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 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"; https://www.tutorialspoint.com/mongodb/mongodb_php.htm 1/5 insert() method is used. $collection = $db->createCollection("mycol"). echo "Database mydb selected".tutorialspoint. Following is the code snippet to insert a document − <?php // connect to mongodb $m = new MongoClient().htm 2/5 . "by". find() method is used. 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. echo "Database mydb selected". echo "Document inserted successfully". ?> When the program is executed. "url" => "http://www. 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.tutorialspoint. echo "Collection created succsessfully".com/mongodb/mongodb_php. $collection = $db->mycol. "likes" => 100. "tutorials point" ). ?> When the program is executed.com/mongodb/". echo "Collection selected succsessfully". echo "Connection to database successfully".2/28/2018 MongoDB PHP // select a database $db = $m->mydb. "description" => "database". // select a database $db = $m->mydb. $collection->insert($document). $document = array( "title" => "MongoDB". https://www. echo "Document updated successfully". echo "Collection selected succsessfully".tutorialspoint. // iterate cursor to display title of documents foreach ($cursor as $document) { echo $document["title"] . we will update the title of inserted document to MongoDB Tutorial. // select a database $db = $m->mydb. } ?> When the program is executed. array('$set'=>array("title"=>"MongoDB Tutorial"))). echo "Connection to database successfully". 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. $collection = $db->mycol. echo "Connection to database successfully". https://www.2/28/2018 MongoDB PHP Following is the code snippet to select all documents − <?php // connect to mongodb $m = new MongoClient(). echo "Database mydb selected".htm 3/5 . Following is the code snippet to update a document − <?php // connect to mongodb $m = new MongoClient(). // now display the updated document $cursor = $collection->find(). In the following example. "\n". // select a database $db = $m->mydb.com/mongodb/mongodb_php. echo "Collection selected succsessfully". $cursor = $collection->find(). $collection = $db->mycol. // now update the document $collection->update(array("title"=>"MongoDB"). you need to use the update() method. echo "Database mydb selected". "\n". // select a database $db = $m->mydb. foreach ($cursor as $document) { echo $document["title"] . you need to use remove() method. // iterate cursor to display title of documents echo "Updated document". echo "Connection to database successfully". echo "Documents deleted successfully".2/28/2018 MongoDB PHP // iterate cursor to display title of documents echo "Updated document". } ?> When the program is executed.false). Following is the code snippet to delete a document − <?php // connect to mongodb $m = new MongoClient(). foreach ($cursor as $document) { echo $document["title"] . "\n". we will remove the documents that has the title MongoDB Tutorial. 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 document. echo "Collection selected succsessfully". it will produce the following result − https://www.htm 4/5 . // now display the available documents $cursor = $collection->find(). } ?> When the program is executed. // now remove the document $collection->remove(array("title"=>"MongoDB Tutorial").tutorialspoint.com/mongodb/mongodb_php. $collection = $db->mycol. echo "Database mydb selected". In the following example.  Previous Page Next Page  Advertisements Tutorials Point (India) Pvt. skip().2/28/2018 MongoDB PHP Connection to database successfully Database mydb selected Collection selected succsessfully Documents deleted successfully In the above example. works same as explained above. sort() etc. Remaining MongoDB methods findOne().tutorialspoint. limit(). Ltd. All Rights Reserved. YouTube 52K Write for us FAQ's Helping Contact © Copyright 2018. Enter email for newsletter go https://www.htm 5/5 .com/mongodb/mongodb_php. save(). the second parameter is boolean type and used for justOne field of remove() method.
Copyright © 2024 DOKUMEN.SITE Inc.