Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

PHP MongoDB Example

PHP MongoDB Example for beginners and professionals with examples on CRUD, insert document, query document, update document, delete document, use database, projection etc.

<< Back to PHP

Php MongoDB Connectivity

Php 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
MongoDB Php mongodb connectivity 1

2) Edit php.ini File

It is store in the apache server directory /etc/php/7.0/apache2/php.ini

$ extension =
MongoDB Php mongodb connectivity 2

3) Install mongo-php library

Following is the preferred way of installing this library with Composer.

$ composer require mongodb/mongodb
MongoDB Php mongodb connectivity 4

4) Create Php Script

// connect.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' =>'' ] );
// Fetching Record
$record = $collection->find( [ 'name' =>'Peter'] );
foreach ($record as $employe) {
echo $employe['name'], ': ', $employe['email']."<br>";

5) Execute Php Script

Execute this script on the localhost server. It will create database and store data into the mongodb.

MongoDB Php mongodb connectivity 5

6) Enter into Mongo Shell

After executing php script, we can see the created database in mongodb.

$ mongo
MongoDB Php mongodb connectivity 6

6.1. Show Database

The following command is used to show databases.

> show dbs
MongoDB Php mongodb connectivity 7

6.2. Show Collection

The following command is used to show collections.

> show collections
MongoDB Php mongodb connectivity 8

6.3. Access Records

> db.employee.find()
MongoDB Php mongodb connectivity 9

Well all set, this is working fine. We can also perform other database operations as well.

Next TopicPython MongoDB

Related Links:

Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf