How to integrate MongoDB and PHP

Code With Travel
2 min readSep 23, 2019

--

PHP & MongoDB

PHP stands for Hypertext Preprocessor. It’s an open-source, server-side, scripting language used for the development of web applications.MongoDB is one of the widely used NoSQL(Not only SQL) databases in the market today. MongoDB is a document database with scalability and flexibility.

Prerequisites
Knowledge of PHP, any database

Go through following steps in order to setup MongoDB with PHP for windows.

1.) Download and install XAMPP on Windows
2.) Check PHP version
3.) Download and Install MongoDB. (https://www.mongodb.com/download-center/community?jmp=nav)
4.) Create folder data\db in a drive where you installed MongoDB. For example, if you installed MongoDB in C:\Program Files86\MongoDB then create C:\data\db folders in order to save records.
5.) Now download the mongo-PHP driver file from https://s3.amazonaws.com/drivers.mongodb.org/php/index.html. You can check compatibility from https://docs.mongodb.com/ecosystem/drivers/php/.
6.) Now enable extension=php_mongodb.dll extension php.ini
7.) Now run mongo command from windows cmd.

Go through following steps in order to setup MongoDB with PHP for ubuntu.

1.) sudo apt-get install mongodb-server
2.) sudo apt-get install php-mongodb
3.) sudo apt-get install php-mongo
4.) extension=php_curl.dll -uncomment
5.) extension=mongo.so — Add this line
6.) extension=php_mongo.dll — Add this line

Now make mongo.php in mongo folder in xampp.

<?php 
// connect to mongodb
$m = new MongoClient();
echo “Connection to database successfully”;
// select a database
$db = $m->database_name;
echo “Database database_name selected”;
// Select a collection
$collection = $db->collection_name;
echo “Collection selected succsessfully”;
?>

Now how to print all data of collection.

<?php
$m = new MongoClient();
$db = $m->database_name;
$collection = $db->collection_name;
$cursor = $collection->find();
// iterate cursor to display title of documents
foreach ($cursor as $document) {
echo “<pre>”;
print_r($document);
}
?>

If you want selected fields and 10 records from collection then use following syntax.

$cursor = $collection->find([], [“_id” => 1,”user_id”=>1,”user_name”=>1])->limit(10)->sort([“created”=> -1]);

Find record using ID .

<?php
$m = new MongoClient();
$db = $m->database_name;
$collection = $db->user_logs;
print_r($collection->findOne(array(‘_id’ => new MongoId(‘5bfd237996f3f4942e8b4575’))));
exit;
?>

Insert the document in MongoDB

<?php
$m = new MongoClient();
$db = $m->database_name;
$collection = $db->user_logs;
$document = array(
“user_id” => “MongoDB”,
“user_name” => “database”,
“user_type” => 100,
“website_name” => “http://www.xyz.com/
);
$collection->insert($document);
?>

Remove document in MongoDB

<?php
$m = new MongoClient();
$db = $m->database_name;
$collection = $db->user_logs;
print_r($collection->remove( array( ‘_id’ => new MongoID( ‘5d886a2196f3f418048b456d’ ) ) ) );
?>

Conclusion: —
There are many functionalities in the library. You can find all information on this library over https://docs.mongodb.com/php-library/current/.

--

--

No responses yet