Monthly Archives: December 2012

Introduction to MongoDB

Databases
MongoDB has databases, collections, and documents. Download an example file.

Collections
Collections correlate to tables within the relational database paradigm.Like tables, indexes
are applied to collections. A collection is a collection of documents and indexes.

Documents
In MongoDB, the primary object is called a document. MongoDB document is a JSON array with a set of keys that map to values.

Installing MongoDB

Connecting to a Database
The default host is localhost, and the default port is 27017.
Connecting to MongoDB database server at localhost port 27017:
$connection = new Mongo();
Connecting to a remote host with optional custom port:
$connection = new Mongo( “190.50.0.80:66001” );

Selecting a Database
MongoDB will create a new database if you try to select a database that doesn’t. Open connection to MongoDB server:
$db = $connection->selectDB(‘workout’);
or
$db = $connection->workout;

CRUD Operations
Create a collection or access to collection:
$user_collection = $db->user;
or
$user_collection= $connection->workout->user;

Creating a Document
$user = array(‘group_access_id’ =>1, ‘first_name’ => ‘Ruslan’, ‘last_name’ => ‘Verbelchuk’,‘phone’ => ”, “e-mail”=>’ruslan.verbelchuk@gmail.com’, “membership”=>”);
$user_collection->insert($user);

Asynchronous update does not return a value after insert a record.
$user_collection->insert($user);

Synchronously update hold execution until it has finished inserting. The application must wait for the database.
$user_collection->insert($user, array(‘safe’ => true));
To access the primary key:
$user_id = $user[‘_id’];

Save a Document
An _id value is specified and exists.
$user_collection->save($user);

Primary Keys and ObjectIds

MongoDB will automatically create a primary key (ObjectIds) for each document.
$user_id->getTimestamp();

Reading a Document
Access the document by the primary key:
$user_data=$user_collection->findone( array(‘_id’=>$user_id) );
echo ‘<br>First Name: ‘ . $user_data[‘first_name’] . ‘<br/>’;
echo ‘Last Name: ‘ . $user_data[‘last_name’] . ‘<br/>’;
echo ‘E-mail: ‘ . $user_data[‘e-mail’] . ‘<br/>’;
echo ‘<br/>’;

A second parameter that is an array of the fields to return:
$user_data=$user_collection->findone( array(‘_id’=>$user_id), array(‘phone’) );
echo “<p>”.print_r($user_data).”</p>”;

Get all documents:
$user_data=$user_collection->find();
echo $user_data->count() . ‘ document(s) found. <br/>’;
foreach ($user_data as $obj) {
echo ‘<br>First Name: ‘ . $obj[‘first_name’] . ‘<br/>’;
echo ‘Last Name: ‘ . $obj[‘last_name’] . ‘<br/>’;
echo ‘E-mail: ‘ . $obj[‘e-mail’] . ‘<br/>’;
echo ‘<br/>’;
}

Updating a Document
Using $set to update a few fields:
$user_collection->update(
array(‘_id’ =>$user_id),
array(‘$set’=>array(‘e-mail’=>’ruslan.verbelchuk@hotmail.com’,’phone’=>’4164440505′) )
);

Use update to add a value:
$user_collection->update(
array(‘first_name’=>’Ruslan’,’last_name’=>’Verbelchuk’),
array(‘$set’=>array(‘location’=>array(
‘country’=>’CA’,
‘province’=>’ON’,
‘city’=>’Toronto’,
‘zip_code’=>’M5M9T6’,
‘street_Name’=>’Yonge’,
‘street_type’=>’Str’,
‘street_direction’=>”,
‘street_number’=>’1450’,
‘street_type’=>”,
‘number_suffix’=>”,
‘unit_suite_apt’=>”,
‘date_time’=>”,
)
) )
);
Append values atomically to an array:
$user_collection->update(
array(‘first_name’=>’Ruslan’,’last_name’=>’Verbelchuk’),
array(‘$push’=>array(‘location’=>array(
‘country’=>’USA’,
‘province’=>’NY’,
‘city’=>’Rochester’,
‘zip_code’=>’12345’,
‘street_Name’=>’University’,
‘street_type’=>’Str’,
‘street_direction’=>”,
‘street_number’=>’23’,
‘street_type’=>”,
‘number_suffix’=>”,
‘unit_suite_apt’=>”,
‘date_time’=>”,
)
) )
);

Deleting a Document
justOne limit the deletion to a single document.
$criteria = array(‘last_name’=> ‘Verbelchuk’);
$user_collection->remove($criteria, array(“justOne” => true) );

Responsive Web Design

Responsive web design (often abbreviated to RWD) is an approach to web design in which a site is crafted to provide an optimal viewing experience—easy reading and navigation with a minimum of resizing, panning, and scrolling—across a wide range of devices (from desktop computer monitors to mobile phones).

5 APIs that will transform the Web in 2013
http://blog.alexmaccaw.com/the-next-web
Changing Times For Web Developers – 6 Tips You Should Read To Survive
http://www.amazedsaint.com/2012/11/changing-times-for-web-developers-6.html

Frameworks:

  1. Bootstrap
  2. YUI and responsive grids:
    http://gridbuilder.herokuapp.com/,
    http://gridbuilder.herokuapp.com/layout.html
    http://www.youtube.com/watch?v=KpyBAQt7hUs, scroll to 43:10
    (although the first talk is very good – if you are familiar with jQuery UI, you will see that YUI one step ahead actually).