Created
April 1, 2011 20:52
-
-
Save gatesvp/898836 to your computer and use it in GitHub Desktop.
Tests a php multi-update with MongoDB
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
| Original Collection | |
| Array | |
| ( | |
| [_id] => MongoId Object | |
| ( | |
| [$id] => 4d963aa56803fa8318000000 | |
| ) | |
| [owner] => 1 | |
| [type] => question | |
| [v] => 1 | |
| ) | |
| Array | |
| ( | |
| [_id] => MongoId Object | |
| ( | |
| [$id] => 4d963aa56803fa8318000001 | |
| ) | |
| [owner] => 2 | |
| [type] => question | |
| [v] => 1 | |
| ) | |
| Array | |
| ( | |
| [_id] => MongoId Object | |
| ( | |
| [$id] => 4d963aa56803fa8318000002 | |
| ) | |
| [owner] => 1 | |
| [type] => question | |
| [v] => 2 | |
| ) | |
| Updated Collection | |
| Array | |
| ( | |
| [_id] => MongoId Object | |
| ( | |
| [$id] => 4d963aa56803fa8318000000 | |
| ) | |
| [owner] => 1 | |
| [type] => abc | |
| [v] => 1 | |
| ) | |
| Array | |
| ( | |
| [_id] => MongoId Object | |
| ( | |
| [$id] => 4d963aa56803fa8318000001 | |
| ) | |
| [owner] => 2 | |
| [type] => question | |
| [v] => 1 | |
| ) | |
| Array | |
| ( | |
| [_id] => MongoId Object | |
| ( | |
| [$id] => 4d963aa56803fa8318000002 | |
| ) | |
| [owner] => 1 | |
| [type] => abc | |
| [v] => 2 | |
| ) |
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{ | |
| $m = new Mongo('localhost:6907'); | |
| $db = $m->abc; | |
| $collection = $db->def; | |
| $collection->drop(); | |
| // Insert test data | |
| $collection->insert( array('owner' => 1, 'type' => 'question', 'v' => 1) ); | |
| $collection->insert( array('owner' => 2, 'type' => 'question', 'v' => 1) ); | |
| $collection->insert( array('owner' => 1, 'type' => 'question', 'v' => 2) ); | |
| // Print data in system | |
| print("Original Collection\n"); | |
| $cursor = $collection->find(); | |
| foreach($cursor as $data){ | |
| print_r($data); | |
| } | |
| // Perform your update | |
| $collection->update( | |
| array('owner' => 1, 'type' => 'question'), | |
| array('$set' => array('type' => 'abc') ), | |
| array('multiple' => true) | |
| ); | |
| print("\nUpdated Collection\n"); | |
| $cursor = $collection->find(); | |
| foreach($cursor as $data){ | |
| print_r($data); | |
| } | |
| } | |
| catch(Exception $e){ | |
| print_r($e->getMessage()); | |
| print("\n"); | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment