Skip to content

Instantly share code, notes, and snippets.

@ruslanverbelchuk
Forked from anonymous/MongoDBIntroduction.php
Created December 17, 2012 21:45
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruslanverbelchuk/4322582 to your computer and use it in GitHub Desktop.
Save ruslanverbelchuk/4322582 to your computer and use it in GitHub Desktop.
Introduction to MongoDB
<?php
if (class_exists('Mongo')){echo "MongoDB is installed<br>";}else{die(" MongoDB is not installed<br>");}
//echo extension_loaded("mongo") ? "loaded\n" : "not loaded\n";
//print_r(get_loaded_extensions());
try {
// open connection to MongoDB server
$conn = new Mongo('localhost');
// access database
$db = $conn->workout;
// access collection
$user_collection = $db->user;
$user = array(
'group_access_id' =>1,
'first_name' => 'Ruslan',
'last_name' => 'Verbelchuk',
'phone' => '4165550505',
'e-mail' => 'ruslan.verbelchuk@gmail.com',
'membership' => '',
);
try {
//$user_collection->insert($user, array('safe' => true));
$user_collection->insert($user);
//To access the primary key
$user_id = $user['_id'];
} catch (MongoCursorException $e) {
die('Error: The update does not succeed ' . $e->getMessage());
} catch (MongoCursorTimeoutException $e) {
die('Error: Unable to perform the operation ' . $e->getMessage());
}
//MongoDB will automatically create a primary key (ObjectIds) for each document.
print_r($user_id->getTimestamp());
//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>";
$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/>';
}
//Using $set to update a few fields
$user_collection->update(
//array( '_id' => new MongoId('50cf7fc8b4a2b2b925000002')),
array('_id' =>$user_id),
array('$set'=>array('e-mail'=>'ruslan.verbelchuk@hotmail.com','phone'=>'4164440505') )
);
//http://www.canadapost.ca/cpotools/apps/fpc/personal/findByCity?execution=e1s1
//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'=>'',
)
) )
);
/*
class User {}
$user = new User();
$user->first_name = 'Tom';
$user->last_name = 'McLow';
$user_collection->save($user);
* */
//Deleting a Document:
//$criteria = array('_id'=> new MongoId('50cf8206b4a2b2702400000e'));
//$criteria = array('last_name'=> 'Verbelchuk');
//$user_collection->remove($criteria, array("justOne" => true) );
// disconnect from server
$conn->close();
} catch (MongoConnectionException $e) {
die('Error connecting to MongoDB server');
} catch (MongoException $e) {
die('Error: ' . $e->getMessage());
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment