-
-
Save Joemires/389224d72b072fffa43c3a43ea9e1828 to your computer and use it in GitHub Desktop.
PHP commands for basic MongoDB operations
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
try { | |
$mongo = new Mongo(); // default host:port | |
$db = $mongo->example; | |
$collection = $db->test; | |
$document = array('x' => 1); | |
$collection->insert($document); | |
print_r($document); | |
} | |
catch(Exception $e) { | |
print($e->getMessage()); | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
try { | |
$mongo = new Mongo(); // default host:port | |
$db = $mongo->example; | |
$collection = $db->test; | |
$collection->drop(); | |
$document = array( '_id' => 'gates', | |
'name' => 'Gaëtan Voyer-Perrault', | |
'friends' => array('bernie', 'alvin'), | |
'followers' => 18, | |
'contact' => array( 'twitter' => '@gatesvp', | |
'email' => '[email protected]') | |
); | |
$collection->insert($document); | |
print_r($document); | |
} | |
catch(Exception $e) { | |
print($e->getMessage()); | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
try { | |
$mongo = new Mongo(); // default host:port | |
$db = $mongo->example; | |
$collection = $db->test; | |
$collection->drop(); | |
$document = array( '_id' => 'gates', | |
'name' => 'Gaëtan Voyer-Perrault', | |
'friends' => array('bernie', 'alvin'), | |
'followers' => 18, | |
'contact' => array( 'twitter' => '@gatesvp', | |
'email' => '[email protected]') | |
); | |
$collection->insert($document); | |
// Basic query | |
$query = array( '_id' => 'gates'); | |
$result = $collection->findOne($query); | |
print($result['_id']."\n"); | |
// Query on array | |
$query = array( 'friends' => 'alvin'); | |
$result = $collection->findOne($query); | |
print($result['_id']."\n"); | |
// Query on sub-document | |
$query = array( 'contact.twitter' => '@gatesvp'); | |
$result = $collection->findOne($query); | |
print($result['_id']."\n"); | |
} | |
catch(Exception $e) { | |
print($e->getMessage()); | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
try { | |
$mongo = new Mongo(); // default host:port | |
$db = $mongo->example; | |
$collection = $db->test; | |
$collection->drop(); | |
$document = array( '_id' => 'gates', | |
'name' => 'Gaëtan Voyer-Perrault', | |
'friends' => array('bernie', 'alvin'), | |
'followers' => 18, | |
'contact' => array( 'twitter' => '@gatesvp', | |
'email' => '[email protected]') | |
); | |
$collection->insert($document); | |
$query = array( '_id' => 'gates'); | |
$fields = array( '_id' => 0, 'name' => 1, 'friends' => 1); | |
$result = $collection->findOne($query, $fields); | |
print_r($result); | |
} | |
catch(Exception $e) { | |
print($e->getMessage()); | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
try { | |
$mongo = new Mongo(); // default host:port | |
$db = $mongo->example; | |
$collection = $db->test; | |
$collection->drop(); | |
$collection->insert(array('x' => 1)); | |
$collection->insert(array('x' => 2)); | |
$collection->insert(array('x' => 3)); | |
$results = $collection->find(); | |
#foreach($results as $r) { print_r($r); } | |
while($results->hasNext()){ | |
$r = $results->getNext(); | |
print_r($r); | |
} | |
} | |
catch(Exception $e) { | |
print($e->getMessage()); | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
try { | |
$mongo = new Mongo(); // default host:port | |
$db = $mongo->example; | |
$collection = $db->test; | |
$collection->drop(); | |
$collection->insert(array('x' => 1)); | |
$collection->insert(array('x' => 2)); | |
$collection->insert(array('x' => 3)); | |
print($collection->find()->count()."\n"); // 3 | |
foreach($collection->find()->limit(1) as $doc){ | |
print_r($doc); | |
} // x=>1 | |
foreach($collection->find()->skip(1)->limit(1) as $doc){ | |
print_r($doc); | |
}// x=>2 | |
} | |
catch(Exception $e) { | |
print($e->getMessage()); | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
try { | |
$mongo = new Mongo(); // default host:port | |
$db = $mongo->example; | |
$collection = $db->test; | |
$collection->drop(); | |
$document = array( '_id' => 'gates', | |
'name' => 'Gaëtan Voyer-Perrault', | |
'friends' => array('bernie', 'alvin'), | |
'followers' => 18, | |
'contact' => array( 'twitter' => '@gatesvp', | |
'email' => '[email protected]') | |
); | |
$collection->insert($document); | |
// update our document | |
$query = array( '_id' => 'gates' ); | |
$update = array( | |
'$set' => array('name' => 'GVP', | |
'contact.website' => 'http://10gen.com/'), | |
'$inc' => array('followers' => 1), | |
'$push' => array('friends' => 'antoine'), | |
'$unset' => array('contact.twitter' => 1) | |
); | |
$collection->update($query, $update); | |
$query = array( '_id' => 'gates'); | |
$result = $collection->findOne($query); | |
print_r($result); | |
} | |
catch(Exception $e) { | |
print($e->getMessage()); | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
try { | |
$mongo = new Mongo(); // default host:port | |
$db = $mongo->example; | |
$collection = $db->test; | |
$collection->drop(); | |
$collection->insert(array('x' => 1)); | |
$collection->insert(array('x' => 1)); | |
$collection->insert(array('x' => 3)); | |
$query = array('x' => 1); | |
$update = array('$inc' => array('x' => 1)); | |
$options = array('multiple' => true); | |
$collection->update($query, $update, $options); | |
$results = $collection->find(); | |
foreach($results as $r) { print_r($r); } | |
} | |
catch(Exception $e) { | |
print($e->getMessage()); | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
try { | |
$mongo = new Mongo(); // default host:port | |
$db = $mongo->example; | |
$collection = $db->test; | |
$collection->drop(); | |
$query = array('_id' => 'gates'); | |
$update = array('$inc' => array('followers' => 1)); | |
$options = array('upsert' => true); | |
$collection->update($query, $update, $options); | |
$results = $collection->find(); | |
foreach($results as $r) { print_r($r); } | |
} | |
catch(Exception $e) { | |
print($e->getMessage()); | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
try { | |
$mongo = new Mongo(); // default host:port | |
$db = $mongo->example; | |
$collection = $db->test; | |
$collection->drop(); | |
$document = array( | |
'normal' => array('first','second'), | |
'crazy' => array("0" => 'first', '1' => 'second'), | |
'arrayObj' => new ArrayObject(array('first', 'second')), | |
'object' => array('1' => 'first', '2' => 'second') | |
); | |
$collection->insert($document); | |
$collection->update(array(), | |
array('$push' => array('crazy' => 'third')) | |
); // works | |
$collection->update(array(), | |
array('$push' => array('normal' => 'third')) | |
); // works | |
$collection->update(array(), | |
array('$push' => array('object' => 'third')) | |
); // works | |
$collection->update(array(), | |
array('$push' => array('arrayObj' => 'third')) | |
); // works | |
$results = $collection->find(); | |
foreach($results as $r) { print_r($r); } | |
} | |
catch(Exception $e) { | |
print($e->getMessage()); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment