note 102830 deleted from mongo.tutorial by danbrown

From: Date: Wed, 09 Mar 2011 14:49:32 +0000
Subject: note 102830 deleted from mongo.tutorial by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-177717@lists.php.net to get a copy of this message
Note Submitter: atul at fd4cast dot com ---- 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); ?>

« previous php.notes (#177717) next »