MongoDB is a free and open-source cross-platform document-oriented database program. It classified as a NoSQL database program, and using JSON-like documents with schemata.

 

Installation Steps:

1. Download and Install MongoDB server for Windows.

https://www.mongodb.com/dr/fastdl.mongodb.org/win32/mongodb-win32-x86_64-2008plusssl-3.6.2-signed.msi/download

2. Download and extract MongoDB PHP driver

https://s3.amazonaws.com/drivers.mongodb.org/php/php_mongo-1.6.8.zip

3. Rename any one file (Eg. php_mongo-1.6.8-5.6-vc11.dll) to php_mongo.dll and copy it to Extension directory known as ext directory.

4. Add the following line to your php.ini

        extension=php_mongo.dll

5. Add Environment variable (Control Panel -> System and Security -> System -> Advanced system settings -> Environment variables) by editing PATH variable.

6. Create directory C:\data\db

7. Restart Apache server


8. Open CMD and start MongoDB server by  using command  Mongod

Basic queries:

  1. MongoDB Connection
$con = new MongoClient();
  1. Selection or Creation of Database (MySQL : Database)
$db = $con->database_name;
  1. Collection Creation (MySQL : Table)
$collection = $db->createCollection("collection_name");
  1. Document Insertion (MySQL : Insert – Row)
$document = array(
"key-1" => "value-1",
"key-n" => "value-n”
);
$collection->insert($document);
  1. View data (MySQL : Select)
$cursor = $collection->find(); 
foreach ($cursor as $document) {
    echo $document["key"];
}
  1. Updating data (MySQL : Update)
$collection->update(array("key"=>"old-value") , array('$set'=>array("key"=>"new-value")));
  1. Deletion of data (MySQL : Delete)
$collection->remove(array("condition-key"=>"condition-value"));

Leave a Reply