|
| 1 | +# MongoDB\Collection |
| 2 | + |
| 3 | +`MongoDB\Collection` is perhaps the most useful class in this library. It |
| 4 | +provides methods for common operations on a collection, such as inserting |
| 5 | +documents, querying, updating, counting, etc. |
| 6 | + |
| 7 | +A Collection may be constructed directly (using the extension's Manager class) |
| 8 | +or selected from the library's Client class. It supports the following options: |
| 9 | + |
| 10 | + * [readConcern](http://php.net/mongodb-driver-readconcern) |
| 11 | + * [readPreference](http://php.net/mongodb-driver-readpreference) |
| 12 | + * [typeMap](http://php.net/manual/en/mongodb.persistence.php#mongodb.persistence.typemaps) |
| 13 | + * [writeConcern](http://php.net/mongodb-driver-writeconcern) |
| 14 | + |
| 15 | +If any options are omitted, they will be inherited from the Manager constructor |
| 16 | +argument or object from which the Collection was selected. |
| 17 | + |
| 18 | +Operations within the Collection class (e.g. `find()`, `insertOne()`) will |
| 19 | +generally inherit the Collection's options. One notable exception to this rule |
| 20 | +is that `aggregate()` (when not using a cursor) and the `findAndModify` variants |
| 21 | +do not yet support a type map option due to a driver limitation. This means that |
| 22 | +they will return BSON documents and arrays as `stdClass` objects and PHP arrays, |
| 23 | +respectively. |
| 24 | + |
| 25 | +## Collection-level Operations |
| 26 | + |
| 27 | +The Collection class has methods for collection-level operations, such as |
| 28 | +dropping the collection, CRUD operations, or managing the collection's indexes. |
| 29 | + |
| 30 | +### Dropping the Collection |
| 31 | + |
| 32 | +``` |
| 33 | +$collection = (new MongoDB\Client)->demo->zips; |
| 34 | +
|
| 35 | +$result = $collection->drop(); |
| 36 | +var_dump($result); |
| 37 | +``` |
| 38 | + |
| 39 | +The above example would output something similar to: |
| 40 | + |
| 41 | +``` |
| 42 | +object(MongoDB\Model\BSONDocument)#11 (1) { |
| 43 | + ["storage":"ArrayObject":private]=> |
| 44 | + array(3) { |
| 45 | + ["ns"]=> |
| 46 | + string(9) "demo.zips" |
| 47 | + ["nIndexesWas"]=> |
| 48 | + int(1) |
| 49 | + ["ok"]=> |
| 50 | + float(1) |
| 51 | + } |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +## CRUD Operations |
| 56 | + |
| 57 | +CRUD is an acronym for Create, Read, Update, and Delete. The Collection class |
| 58 | +implements MongoDB's cross-driver |
| 59 | +[CRUD specification](https://github.com/mongodb/specifications/blob/master/source/crud/crud.rst), |
| 60 | +which defines a common API for collection-level read and write methods. |
| 61 | + |
| 62 | +Each method on the Collection class corresponds to a particular Operation class |
| 63 | +within the library. The Collection's method merely merges in relevant options |
| 64 | +(e.g. read preferences, type maps). Documentation for each CRUD method and its |
| 65 | +options may be found in either the CRUD specification or those Operation |
| 66 | +classes. |
| 67 | + |
| 68 | +### API Differences from the Legacy Driver |
| 69 | + |
| 70 | +The CRUD API has some notable differences from the legacy driver's |
| 71 | +[MongoCollection](http://php.net/mongocollection) class: |
| 72 | + |
| 73 | + * `insert()` and `batchInsert()` have been renamed to `insertOne()` and |
| 74 | + `insertMany()`, respectively. |
| 75 | + * `update()` has been split into `updateOne()`, `updateMany()`, and |
| 76 | + `replaceOne()`. |
| 77 | + * `remove()` has been split into `deleteOne()` and `deleteMany()`. |
| 78 | + * `findAndModify()` has been split into `findOneAndDelete()`, |
| 79 | + `findOneAndReplace()`, and `findOneAndUpdate()`. |
| 80 | + * `save()`, which was syntactic sugar for an insert or upsert operation, has |
| 81 | + been removed in favor of explicitly using `insertOne()` or `replaceOne()` |
| 82 | + (with the `upsert` option). |
| 83 | + * `aggregate()` and `aggregateCursor()` have been consolidated into a single |
| 84 | + `aggregate()` method. |
| 85 | + * A general-purpose `bulkWrite()` method replaces the legacy driver's |
| 86 | + [`MongoWriteBatch`](http://php.net/mongowritebatch) class. |
| 87 | + |
| 88 | +The general rule in designing our new API was that explicit method names were |
| 89 | +preferable to overloaded terms found in the old API. For instance, `save()` and |
| 90 | +`findAndModify()` had two or three very different modes of operation, depending |
| 91 | +on their arguments. These new methods names also distinguish between |
| 92 | +[updating specific fields](https://docs.mongodb.org/manual/tutorial/modify-documents/#update-specific-fields-in-a-document) |
| 93 | +and [full-document replacement](https://docs.mongodb.org/manual/tutorial/modify-documents/#replace-the-document). |
| 94 | + |
| 95 | +### Finding One or More Document(s) |
| 96 | + |
| 97 | +The `findOne()` and `find()` methods may be used to query for one or multiple |
| 98 | +documents. |
| 99 | + |
| 100 | +``` |
| 101 | +$collection = (new MongoDB\Client)->demo->zips; |
| 102 | +
|
| 103 | +$document = $collection->findOne(['_id' => '94301']); |
| 104 | +var_dump($document); |
| 105 | +``` |
| 106 | + |
| 107 | +The above example would output something similar to: |
| 108 | + |
| 109 | +``` |
| 110 | +object(MongoDB\Model\BSONDocument)#13 (1) { |
| 111 | + ["storage":"ArrayObject":private]=> |
| 112 | + array(5) { |
| 113 | + ["_id"]=> |
| 114 | + string(5) "94301" |
| 115 | + ["city"]=> |
| 116 | + string(9) "PALO ALTO" |
| 117 | + ["loc"]=> |
| 118 | + object(MongoDB\Model\BSONArray)#12 (1) { |
| 119 | + ["storage":"ArrayObject":private]=> |
| 120 | + array(2) { |
| 121 | + [0]=> |
| 122 | + float(-122.149685) |
| 123 | + [1]=> |
| 124 | + float(37.444324) |
| 125 | + } |
| 126 | + } |
| 127 | + ["pop"]=> |
| 128 | + int(15965) |
| 129 | + ["state"]=> |
| 130 | + string(2) "CA" |
| 131 | + } |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +The `find()` method returns a |
| 136 | +[`MongoDB\Driver\Cursor`](http://php.net/mongodb-driver-cursor) object, which |
| 137 | +may be iterated upon to access all matched documents. |
| 138 | + |
| 139 | +## Index Management |
| 140 | + |
| 141 | +The Collection class implements MongoDB's cross-driver |
| 142 | +[Index Management](https://github.com/mongodb/specifications/blob/master/source/index-management.rst) |
| 143 | +and |
| 144 | +[Enumerating Indexes](https://github.com/mongodb/specifications/blob/master/source/enumerate-indexes.rst) |
| 145 | +specifications, which defines a common API for index-related methods. |
| 146 | + |
| 147 | +### Creating Indexes |
| 148 | + |
| 149 | +``` |
| 150 | +$collection = (new MongoDB\Client)->demo->zips; |
| 151 | +
|
| 152 | +$result = $collection->createIndex(['state' => 1]); |
| 153 | +var_dump($result); |
| 154 | +``` |
| 155 | + |
| 156 | +The above example would output something similar to: |
| 157 | + |
| 158 | +``` |
| 159 | +string(7) "state_1" |
| 160 | +``` |
| 161 | + |
| 162 | +### Dropping Indexes |
| 163 | + |
| 164 | +``` |
| 165 | +$collection = (new MongoDB\Client)->demo->zips; |
| 166 | +
|
| 167 | +$result = $collection->dropIndex('state_1'); |
| 168 | +var_dump($result); |
| 169 | +``` |
| 170 | + |
| 171 | +The above example would output something similar to: |
| 172 | + |
| 173 | +``` |
| 174 | +object(MongoDB\Model\BSONDocument)#11 (1) { |
| 175 | + ["storage":"ArrayObject":private]=> |
| 176 | + array(2) { |
| 177 | + ["nIndexesWas"]=> |
| 178 | + int(2) |
| 179 | + ["ok"]=> |
| 180 | + float(1) |
| 181 | + } |
| 182 | +} |
| 183 | +``` |
| 184 | + |
| 185 | +### Enumerating Indexes |
| 186 | + |
| 187 | +``` |
| 188 | +/* listIndexes() returns an iterator of MongoDB\Model\IndexInfo objects */ |
| 189 | +$collection = (new MongoDB\Client)->demo->zips; |
| 190 | +
|
| 191 | +foreach ($collection->listIndexes() as $indexInfo) { |
| 192 | + var_dump($indexInfo); |
| 193 | +} |
| 194 | +``` |
| 195 | + |
| 196 | +The above example would output something similar to: |
| 197 | + |
| 198 | +``` |
| 199 | +object(MongoDB\Model\IndexInfo)#4 (4) { |
| 200 | + ["v"]=> |
| 201 | + int(1) |
| 202 | + ["key"]=> |
| 203 | + array(1) { |
| 204 | + ["_id"]=> |
| 205 | + int(1) |
| 206 | + } |
| 207 | + ["name"]=> |
| 208 | + string(4) "_id_" |
| 209 | + ["ns"]=> |
| 210 | + string(9) "demo.zips" |
| 211 | +} |
| 212 | +``` |
0 commit comments