C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Php MongoDB ConnectivityPhp provides mongodb driver to connect with mongoDB database. After installing it, we can perform database operations by using the php. Here, we are using Ubuntu 16.04 to create an example. This example includes the following steps. 1) Installing Driver$ pecl install mongodb 2) Edit php.ini FileIt is store in the apache server directory /etc/php/7.0/apache2/php.ini $ extension = mongodb.so 3) Install mongo-php libraryFollowing is the preferred way of installing this library with Composer. $ composer require mongodb/mongodb 4) Create Php Script// connect.php <?php require 'vendor/autoload.php'; // Creating Connection $con = new MongoDB\Client("mongodb://localhost:27017"); // Creating Database $db = $con->TheDeveloperBlog; // Creating Document $collection = $db->employee; // Insering Record $collection->insertOne( [ 'name' =>'Peter', 'email' =>'peter@abc.com' ] ); // Fetching Record $record = $collection->find( [ 'name' =>'Peter'] ); foreach ($record as $employe) { echo $employe['name'], ': ', $employe['email']."<br>"; } ?> 5) Execute Php ScriptExecute this script on the localhost server. It will create database and store data into the mongodb. localhost/php/connect.php 6) Enter into Mongo ShellAfter executing php script, we can see the created database in mongodb. $ mongo 6.1. Show Database The following command is used to show databases. > show dbs 6.2. Show Collection The following command is used to show collections. > show collections 6.3. Access Records > db.employee.find() Well all set, this is working fine. We can also perform other database operations as well.
Next TopicPython MongoDB
|