MongoDB in php

The last database type that we will look at is known as a NoSQL type of database. NoSQL databases are on the rise in popularity because they are also quite lightweight in terms of system resources, but more importantly, they work outside the typical SQL command structure. NoSQL DBs are also becoming more popular with mobile devices like tablets and smartphones

Example of MongoDB library

$mongo = new Mongo();
$db = $mongo->library;
$authors = $db->authors;
$author = array('authorid' => 1, 'name' => "J.R.R. Tolkien");
$authors->insert($author);
$author = array('authorid' => 2, 'name' => "Alex Haley");
$authors->insert($author);
$author = array('authorid' => 3, 'name' => "Tom Clancy");
$authors->save($author);
$author = array('authorid' => 4, 'name' => "Isaac Asimov");
$authors->save($author);

Retrieving Data in MongoDB

$mongo = new Mongo();
$db = $mongo->library;
$authors = $db->authors;
$data = $authors->findone(array('authorid' => 4));
echo "Generated Primary Key: {$data['_id']}<br />";
echo "Author name: {$data['name']}";

Leave a Comment