|
| 1 | +# Basic CRUD |
| 2 | + |
| 3 | +```php |
| 4 | +<?php |
| 5 | + |
| 6 | +/* The document we'll be working with */ |
| 7 | +$document = array("hello" => "world"); |
| 8 | + |
| 9 | +/* We want to gurantee all writes to the majority of our nodes */ |
| 10 | +$w = MongoDB\WriteConcern::MAJORITY; |
| 11 | + |
| 12 | +/* But don't be waiting on me fore more then 1000ms (1sec), |
| 13 | + * I have an application to run! */ |
| 14 | +$wtimeout = 1000; |
| 15 | + |
| 16 | +/* No need to journal or fsync (are infact discouraged in general) */ |
| 17 | +$journal = $fsync = false; |
| 18 | + |
| 19 | +/* Construct the WriteConcern object from our options */ |
| 20 | +$wc = new MongoDB\WriteConcern($w, $wtimeout, $journal, $fsync); |
| 21 | + |
| 22 | + |
| 23 | +/* We prefer to read from the secondary, but are OK to read from the primary |
| 24 | + * if we can't find any secondaries */ |
| 25 | +$prefer = MongoDB\ReadPreference::RP_SECONDARY_PREFERRED; |
| 26 | +$tags = array( |
| 27 | + /* Prefer the West Coast datacenter in Iceland */ |
| 28 | + array("country" => "iceland", "datacenter" => "west"), |
| 29 | + |
| 30 | + /* Fallback to any datacenter in Iceland */ |
| 31 | + array("country" => "iceland"), |
| 32 | + |
| 33 | + /* If Iceland is offline, read from whatever is online! */ |
| 34 | + array(), |
| 35 | + ); |
| 36 | + |
| 37 | +/* Construct the ReadPreference object from our options */ |
| 38 | +$rp = new MongoDB\ReadPreference($prefer, $tags); |
| 39 | + |
| 40 | + |
| 41 | +/* Construct the MongoDB Manager */ |
| 42 | +$manager = new MongoDB\Manager("mongodb://localhost:27017"); |
| 43 | + |
| 44 | +?> |
| 45 | +``` |
| 46 | + |
| 47 | +## CREATE |
| 48 | + |
| 49 | +```php |
| 50 | +<?php |
| 51 | + |
| 52 | +try { |
| 53 | + /* Full namespace as the first argumnet (databasename.collectionname), |
| 54 | + * and the document to insert as the second. |
| 55 | + * |
| 56 | + * Returns MongoDB\WriteResult on success, throws exception on failure |
| 57 | + */ |
| 58 | + $result = $manager->executeInsert("db.collection", $document, $wc); |
| 59 | +} catch(Exception $e) { |
| 60 | + echo $e->getMessage(), "\n"; |
| 61 | + exit; |
| 62 | +} |
| 63 | + |
| 64 | +?> |
| 65 | +``` |
| 66 | + |
| 67 | +## READ |
| 68 | + |
| 69 | +```php |
| 70 | +<?php |
| 71 | + |
| 72 | +/* Construct an empty find query ("select all") */ |
| 73 | +$query = new MongoDB\Query(array()); |
| 74 | + |
| 75 | +try { |
| 76 | + /* Full namespace as the first argument (dbname.collname), and the query object |
| 77 | + * to execute as the second. |
| 78 | + * Returns MongoDB\QueryResult on success, throws exception on failure |
| 79 | + */ |
| 80 | + $cursor = $manager->executeQuery("db.collection", $query, $rp); |
| 81 | + |
| 82 | + /* Iterate over all matched documents */ |
| 83 | + foreach($cursor as $document) { |
| 84 | + var_dump($document); |
| 85 | + } |
| 86 | +} catch(Exception $e) { |
| 87 | + echo $e->getMessage(), "\n"; |
| 88 | + exit; |
| 89 | +} |
| 90 | + |
| 91 | +?> |
| 92 | +``` |
| 93 | + |
| 94 | +## UPDATE |
| 95 | + |
| 96 | +```php |
| 97 | +<?php |
| 98 | + |
| 99 | +/* The search criteria */ |
| 100 | +$where = array("hello" => "world"); |
| 101 | + |
| 102 | +/* What to update the matched document with */ |
| 103 | +$set = array('$set' => array("hello" => "wonderful world")); |
| 104 | + |
| 105 | +/* Special MongoDB wireprotocol options: |
| 106 | + * limit: integer. Updates all matching document when 0 (false). |
| 107 | + * Only the first matching document when > 0 (true). |
| 108 | + * upsert: boolean. If there is no matching document, create a new document |
| 109 | + * from the $where and apply the $set |
| 110 | + */ |
| 111 | +$options = array("limit" => 1, "upsert" => false); |
| 112 | +try { |
| 113 | + /* Full namespace as the first argument (dbname.collname), the search criteria |
| 114 | + * second. |
| 115 | + * Returns MongoDB\WriteResult on success, throws exception on failure |
| 116 | + */ |
| 117 | + $result = $manager->executeUpdate("db.collection", $where, $set, $options, $wc); |
| 118 | + var_dump($result); |
| 119 | +} catch(Exception $e) { |
| 120 | + echo $e->getMessage(), "\n"; |
| 121 | + exit; |
| 122 | +} |
| 123 | + |
| 124 | +?> |
| 125 | +``` |
| 126 | + |
| 127 | +## DELETE |
| 128 | + |
| 129 | +```php |
| 130 | +<?php |
| 131 | + |
| 132 | +/* The search criteria */ |
| 133 | +$where = array("hello" => "world"); |
| 134 | + |
| 135 | +/* Special MongoDB wireprotocol options: |
| 136 | + * limit: integer. Updates all matching document when > 0 (true). |
| 137 | + * Only the first matching document when 0 (false). |
| 138 | + */ |
| 139 | +$options = array("limit" => 1); |
| 140 | +try { |
| 141 | + /* |
| 142 | + /* Full namespace as the first argument (dbname.collname), the search criteria |
| 143 | + * second. |
| 144 | + * Returns MongoDB\WriteResult on success, throws exception on failure |
| 145 | + */ |
| 146 | + $result = $manager->executeDelete("db.collection", $where, $options, $wc); |
| 147 | + var_dump($result); |
| 148 | +} catch(Exception $e) { |
| 149 | + echo $e->getMessage(), "\n"; |
| 150 | + exit; |
| 151 | +} |
| 152 | + |
| 153 | +?> |
| 154 | +``` |
| 155 | + |
| 156 | + |
0 commit comments