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) );

This entry was posted in MongoDB and tagged . Bookmark the permalink.