note 102830 added to mongo.tutorial
| From: | atul at fd4cast dot com | Date: | Wed, 09 Mar 2011 08:06:08 +0000 |
| Subject: | note 102830 added to mongo.tutorial | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-177678@lists.php.net to get a copy of this message | ||
Anyone who is serious about using MongoDB will almost certainly host the DB with a DBaaS provider.
You can find a current list of services at http://www.mongodb.org/display/DOCS/Hosting+Center.
I played around with MongoDB on my local WAMP server extensively before deciding to give one of
these services, MongoHQ, a try. I immediately ran into trouble with connecting to the DB,
populating it, querying it etc. So here is a short code snippet to get you started if you are going
down the same route.
<?php
try
{
$mongo = new mongo("mongodb://USER:PASSWORD@SERVER.mongohq.com:PORT/DATABASE");
/*
The uppercase text here must be replaced with appropriate values.
USER & PASSWORD are set from your MongoHQ account settings.
After creating your MongoHQ account, create a test database. Once this is done you will be given the
name of the server on which it is hosted (usually Flame) and the port on which it is to be accessed
*/
}
catch(MongoConnectionException $e)
{
die('Could not connect. Check to make sure MongoDB is running.');
}
$db = $mongo->DATABASE;//change to your database name
$col = $db->order;
try
{
$doc = array('login' =>'jsmith','password' =>
'5f4dcc3b5aa765d61d8327deb882cf99','email' =>
'jsmith@example.com');
$col->insert($doc, true);
$doc = array('login' => 'psmith','password' =>
'319f4d26e3c536b5dd871bb2c52e3178','email' =>
'psmith@example.com');
$col->insert($doc, true);
}
catch(MongoCursorException $e)
{
echo 'We have a problem';
}
// Get the id of last insert
$id = $doc['_id'];
// Get all documents
$res = $col->find();
echo 'All documents:--------------------------------<br>';
foreach($res as $doc)
{
echo '<pre>';
print_r($doc);
echo '</pre>';
}
// Query for the document matching the last insert ID
$doc = $col->findone(array('_id' => $id));
echo 'Single document (_id = $id):-----------------------<br><pre>';
print_r($doc);
// Update a document
$col->update(array('_id' => $id), array('$set' =>
array('password' => '44c7be48226ebad5dca8216674cad62b')));
// Query the updated docuemnt
$doc = $col->findone(array('_id' => $id));
echo 'Updated docuement:<br/><pre>';
print_r($doc);
echo '</pre>';
echo "Javascript Query--------------------------<br>";
$js = "function(){return this.login == 'jsmith'}";
$query = array('$where'=>$js);
$cursor = $col->find($query);
foreach ($cursor as $family) print_r($family);
?>
----
Server IP: 69.147.83.197
Probable Submitter: 88.207.159.115
----
Manual Page -- http://www.php.net/manual/en/mongo.tutorial.php
Edit -- https://master.php.net/note/edit/102830
Del: integrated -- https://master.php.net/note/delete/102830/integrated
Del: useless -- https://master.php.net/note/delete/102830/useless
Del: bad code -- https://master.php.net/note/delete/102830/bad+code
Del: spam -- https://master.php.net/note/delete/102830/spam
Del: non-english -- https://master.php.net/note/delete/102830/non-english
Del: in docs -- https://master.php.net/note/delete/102830/in+docs
Del: other reasons-- https://master.php.net/note/delete/102830
Reject -- https://master.php.net/note/reject/102830
Search -- https://master.php.net/manage/user-notes.php