PHP & MongoDB – Select Document

PHP & MongoDB – Select Document ”; Previous Next First step to do any operation is to create a Manager instance. // Connect to MongoDB using Manager Instance $manager = new MongoDBDriverManager(“mongodb://localhost:27017″); Second step is to prepare and execute a Query object to select record(s) in the collection. // Create a Query Object $query = new MongoDBDriverQuery([”First_Name” => “Radhika”]); // Execute the query $rows = $manager->executeQuery(“testdb.sampleCollection”, $query); Example Try the following example to select a document in MongoDB server − Copy and paste the following example as mongodb_example.php − <?php try { // connect to mongodb $manager = new MongoDBDriverManager(“mongodb://localhost:27017”); $filter = []; // Create a Query Object $query = new MongoDBDriverQuery($filter); // Execute the query $rows = $manager->executeQuery(“myDb.sampleCollection”, $query); foreach ($rows as $row) { printf(“First Name: %s, Last Name: %s.<br/>”, $row->First_Name, $row->Last_Name); } } catch (MongoDBDriverExceptionException $e) { echo “Exception:”, $e->getMessage(), “n”; } ?> Output Access the mongodb_example.php deployed on apache web server and verify the output. First Name: Radhika, Last Name: Sharma. First Name: Rachel, Last Name: Christopher. First Name: Fathima, Last Name: Sheik. Print Page Previous Next Advertisements ”;

PHP & MongoDB – Drop Database

PHP & MongoDB – Drop Database ”; Previous Next First step to do any operation is to create a Manager instance. // Connect to MongoDB using Manager Instance $manager = new MongoDBDriverManager(“mongodb://localhost:27017”); Second step is to prepare and execute a command to drop the database. // Create a Command Instance $dropDatabase = new MongoDBDriverCommand([“dropDatabase” => 1]); // Execute the command on the database $cursor = $manager->executeCommand(“myDb”, $dropDatabase); Example Try the following example to delete a database in MongoDB server − Copy and paste the following example as mongodb_example.php − <?php try { // connect to mongodb $manager = new MongoDBDriverManager(“mongodb://localhost:27017”); // Create a Command Instance $dropDatabase = new MongoDBDriverCommand([“dropDatabase” => 1]); // Execute the command on the database $cursor = $manager->executeCommand(“myDb”, $dropDatabase); echo “Database dropped.” } catch (MongoDBDriverExceptionException $e) { echo “Exception:”, $e->getMessage(), “n”; } ?> Output Access the mongodb_example.php deployed on apache web server and verify the output. Database dropped. Print Page Previous Next Advertisements ”;

PHP & MongoDB – Connect Database

PHP & MongoDB – Connecting Database ”; Previous Next First step to do any operation is to create a Manager instance. // Connect to MongoDB using Manager Instance $manager = new MongoDBDriverManager(“mongodb://localhost:27017″); Second step is to prepare and execute a command on a database if the database doesn”t exist then MongoDB creates it automatically. // Create a Command Instance $statistics = new MongoDBDriverCommand([“dbstats” => 1]); // Execute the command on the database $cursor = $manager->executeCommand(“myDb”, $statistics); Example Try the following example to connect to a MongoDB server − Copy and paste the following example as mongodb_example.php − <?php try { // connect to mongodb $manager = new MongoDBDriverManager(“mongodb://localhost:27017”); echo “Connection to database successfully”; $statistics = new MongoDBDriverCommand([“dbstats” => 1]); $cursor = $manager->executeCommand(“myDb”, $statistics); $statistics = current($cursor->toArray()); echo “<pre>”; print_r($statistics); echo “</pre>”; } catch (MongoDBDriverExceptionException $e) { echo “Exception:”, $e->getMessage(), “n”; } ?> Output Access the mongodb_example.php deployed on apache web server and verify the output. Connection to database successfully stdClass Object ( [db] => myDb [collections] => 0 [views] => 0 [objects] => 0 [avgObjSize] => 0 [dataSize] => 0 [storageSize] => 0 [totalSize] => 0 [indexes] => 0 [indexSize] => 0 [scaleFactor] => 1 [fileSize] => 0 [fsUsedSize] => 0 [fsTotalSize] => 0 [ok] => 1 ) Print Page Previous Next Advertisements ”;

PHP & MongoDB – Show Databases

PHP & MongoDB – Show Databases ”; Previous Next First step to do any operation is to create a Manager instance. // Connect to MongoDB using Manager Instance $manager = new MongoDBDriverManager(“mongodb://localhost:27017”); Second step is to prepare and execute a command on a database to show the list of databases available. // Create a Command Instance $databaseList = new MongoDBDriverCommand([“listDatabases” => 1]); // Execute the command on the database $cursor = $manager->executeCommand(“admin”, $databaseList); Example Try the following example to list databases available by default in MongoDB server − Copy and paste the following example as mongodb_example.php − <?php try { // connect to mongodb $manager = new MongoDBDriverManager(“mongodb://localhost:27017”); // Create a Command Instance $databaseList = new MongoDBDriverCommand([“listDatabases” => 1]); // Execute the command on the database $cursor = $manager->executeCommand(“admin”, $databaseList); $databases = current($cursor->toArray()); foreach ($databases->databases as $database) { echo $database->name . “<br/>”; } } catch (MongoDBDriverExceptionException $e) { echo “Exception:”, $e->getMessage(), “n”; } ?> Output Access the mongodb_example.php deployed on apache web server and verify the output. admin config local Print Page Previous Next Advertisements ”;

PHP & MongoDB – Display Collections

PHP & MongoDB – Display Collections ”; Previous Next First step to do any operation is to create a Manager instance. // Connect to MongoDB using Manager Instance $manager = new MongoDBDriverManager(“mongodb://localhost:27017”); Second step is to prepare and execute a command on a database to show the list of collections available. // Create a Command Instance $collectionList = new MongoDBDriverCommand([“listCollections” => 1]); // Execute the command on the database $cursor = $manager->executeCommand(“myDb”, $collectionList); Example Try the following example to list collections of a database in MongoDB server − Copy and paste the following example as mongodb_example.php − <?php try { // connect to mongodb $manager = new MongoDBDriverManager(“mongodb://localhost:27017”); // Create a Command Instance $collectionList = new MongoDBDriverCommand([“listCollections” => 1]); // Execute the command on the database $cursor = $manager->executeCommand(“myDb”, $collectionList); $collections = $cursor->toArray(); foreach ($collections as $collection) { echo $collection->name . “<br/>”; } } catch (MongoDBDriverExceptionException $e) { echo “Exception:”, $e->getMessage(), “n”; } ?> Output Access the mongodb_example.php deployed on apache web server and verify the output. sampleCollection Print Page Previous Next Advertisements ”;

PHP & MongoDB – Environment Setup

PHP & MongoDB – Environment Setup ”; Previous Next Install MongoDB database Follow the MongoDB installation steps using MongoDB – Environment Setup Install PHP Follow the PHP installation steps using PHP 7 – Environment Setup PHP MongoDB Driver 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 In case of Windows, make sure libsasl.dll is in the windows”s PATH. This dll is available in PHP installation directory. Print Page Previous Next Advertisements ”;

PHP & MongoDB – Home

PHP & MongoDB Tutorial PDF Version Quick Guide Resources Job Search Discussion PHP based application can connect to MongoDB using PHP MongoDB Driver. PHP MongoDB Driver works with PHP on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX. Audience This tutorial is designed for PHP programmers who would like to understand the MongoDB driver to connect to MongoDB in detail along with its architecture and actual usage. Prerequisites Before proceeding with this tutorial, you should have a good understanding of PHP programming language. As you are going to deal with MongoDB database, you should have prior exposure to NoSQL and Database concepts. Print Page Previous Next Advertisements ”;

PHP & MongoDB – Overview

PHP & MongoDB – Overview ”; Previous Next PHP developer team has provided MongoDB Driver for PHP and have various resources available for it. First step in connecting to MongoDB using PHP is to have mongodb PHP driver dll in php ext directory and enable it in php.ini and then use mongodb API to connect to the database. Connecting to MongoDB database Suppose, MongoDB is installed locally and using default port then following syntax connects to MongoDB database. $manager = new MongoDBDriverManager(“mongodb://localhost:27017”); MongoDB Driver Manager is the central class to do all the operations on mongodb database. To connect to mongodb database, we connect to database by first preparing a command and then executing it. $statistics = new MongoDBDriverCommand([“dbstats” => 1]); $cursor = $manager->executeCommand(“mydb”, $statistics); Once command is executed, if mydb database is not present, it will be created otherwise, it will be connected. Print Page Previous Next Advertisements ”;

PHP & MongoDB – Create Collection

PHP & MongoDB – Create Collection ”; Previous Next First step to do any operation is to create a Manager instance. // Connect to MongoDB using Manager Instance $manager = new MongoDBDriverManager(“mongodb://localhost:27017”); Second step is to prepare and execute a command to create the collection. // Create a Command Instance $createCollection = new MongoDBDriverCommand([“create” => “sampleCollection”]); // Execute the command on the database $cursor = $manager->executeCommand(“myDb”, $createCollection); Example Try the following example to create a collection in MongoDB server − Copy and paste the following example as mongodb_example.php − <?php try { // connect to mongodb $manager = new MongoDBDriverManager(“mongodb://localhost:27017”); // Create a Command Instance $createCollection = new MongoDBDriverCommand([“create” => “sampleCollection”]); // Execute the command on the database $cursor = $manager->executeCommand(“myDb”, $createCollection); echo “Collection created.”; } catch (MongoDBDriverExceptionException $e) { echo “Exception:”, $e->getMessage(), “n”; } ?> Output Access the mongodb_example.php deployed on apache web server and verify the output. Collection created. Print Page Previous Next Advertisements ”;

PHP & MongoDB – Error Handling

PHP & MongoDB – Error Handling ”; Previous Next MongoDB driver operations throws exceptions in case of any database operations and can be captured easily using following syntax: try { } catch (MongoDBDriverExceptionException $e) { echo “Exception:”, $e->getMessage(), “<br/>”; echo “File:”, $e->getFile(), “<br/>”; echo “Line:”, $e->getLine(), “<br/>”; } Example Try the following example to check database operation error in MongoDB server − Copy and paste the following example as mongodb_example.php − <?php try { // connect to mongodb $manager = new MongoDBDriverManager(“mongodb://localhost:27017”); // Create a Command Instance $createCollection = new MongoDBDriverCommand([“create” => “sampleCollection”]); // Execute the command on the database $cursor = $manager->executeCommand(“myDb”, $createCollection); echo “Collection created.”; } catch (MongoDBDriverExceptionException $e) { echo “Exception:”, $e->getMessage(), “<br/>”; echo “File:”, $e->getFile(), “<br/>”; echo “Line:”, $e->getLine(), “<br/>”; } ?> Output Access the mongodb_example.php deployed on apache web server and verify the output. Exception:Collection already exists. NS: myDb.sampleCollection File: htdocsphp_mongodbmongodb_example.php Line:10 Print Page Previous Next Advertisements ”;