@@ -7,7 +7,7 @@ Upgrade Guide
7
7
.. contents:: On this page
8
8
:local:
9
9
:backlinks: none
10
- :depth: 1
10
+ :depth: 2
11
11
:class: singlecol
12
12
13
13
Overview
@@ -119,14 +119,23 @@ new specifications, the new class provides the same functionality as the legacy
119
119
driver's :php:`MongoCollection <mongocollection>` class with some notable
120
120
exceptions.
121
121
122
- Old and New Methods
123
- ~~~~~~~~~~~~~~~~~~~
122
+ A guiding principle in designing the new APIs was that explicit method names are
123
+ preferable to overloaded terms found in the old API. For instance,
124
+ :php:`MongoCollection::save() <mongocollection.save>` and
125
+ :php:`MongoCollection::findAndModify() <mongocollection.findandmodify>`
126
+ have different modes of operation, depending on their arguments. Methods were
127
+ also split to distinguish between :manual:`updating specific fields
128
+ </tutorial/modify-documents>` and :manual:`full-document replacement
129
+ </tutorial/modify-documents/#replace-the-document>`.
130
+
131
+ The following table lists all legacy methods alongside the
132
+ equivalent method(s) in the new driver.
124
133
125
134
.. list-table::
126
135
:header-rows: 1
127
136
128
- * - :php:`MongoCollection <mongocollection>`
129
- - :phpclass:`MongoDB\\Collection`
137
+ * - :php:`MongoCollection <mongocollection>` method
138
+ - :phpclass:`MongoDB\\Collection` method(s)
130
139
131
140
* - :php:`MongoCollection::aggregate() <mongocollection.aggregate>`
132
141
- :phpmethod:`MongoDB\\Collection::aggregate()`
@@ -141,7 +150,7 @@ Old and New Methods
141
150
- :phpmethod:`MongoDB\\Collection::count()`
142
151
143
152
* - :php:`MongoCollection::createDBRef() <mongocollection.createdbref>`
144
- - Not yet implemented. See :issue:`PHPLIB-24`.
153
+ - Not yet implemented. [3]_
145
154
146
155
* - :php:`MongoCollection::createIndex() <mongocollection.createindex>`
147
156
- :phpmethod:`MongoDB\\Collection::createIndex()`
@@ -173,7 +182,7 @@ Old and New Methods
173
182
- :phpmethod:`MongoDB\\Collection::findOne()`
174
183
175
184
* - :php:`MongoCollection::getDBRef() <mongocollection.getdbref>`
176
- - Not implemented. See :issue:`PHPLIB-24`.
185
+ - Not implemented. [3]_
177
186
178
187
* - :php:`MongoCollection::getIndexInfo() <mongocollection.getindexinfo>`
179
188
- :phpmethod:`MongoDB\\Collection::listIndexes()`
@@ -191,7 +200,8 @@ Old and New Methods
191
200
- Not implemented.
192
201
193
202
* - :php:`MongoCollection::group() <mongocollection.group>`
194
- - Not implemented. Use :phpmethod:`MongoDB\\Database::command()`.
203
+ - Not implemented. Use :phpmethod:`MongoDB\\Database::command()`. See
204
+ `Group Command Helper`_ for an example.
195
205
196
206
* - :php:`MongoCollection::insert() <mongocollection.insert>`
197
207
- :phpmethod:`MongoDB\\Collection::insertOne()`
@@ -209,13 +219,13 @@ Old and New Methods
209
219
option.
210
220
211
221
* - :php:`MongoCollection::setReadPreference() <mongocollection.setreadpreference>`
212
- - Not implemented. Use :phpmethod:`MongoDB\\Collection::withOptions()`
222
+ - Not implemented. Use :phpmethod:`MongoDB\\Collection::withOptions()`.
213
223
214
224
* - :php:`MongoCollection::setSlaveOkay() <mongocollection.getslaveokay>`
215
225
- Not implemented.
216
226
217
227
* - :php:`MongoCollection::setWriteConcern() <mongocollection.setwriteconcern>`
218
- - Not implemented. Use :phpmethod:`MongoDB\\Collection::withOptions()`
228
+ - Not implemented. Use :phpmethod:`MongoDB\\Collection::withOptions()`.
219
229
220
230
* - :php:`MongoCollection::update() <mongocollection.update>`
221
231
- :phpmethod:`MongoDB\\Collection::replaceOne()`,
@@ -225,67 +235,6 @@ Old and New Methods
225
235
* - :php:`MongoCollection::validate() <mongocollection.validate>`
226
236
- Not implemented.
227
237
228
- A guiding principle in designing the new APIs was that explicit method names are
229
- preferable to overloaded terms found in the old API. For instance,
230
- :php:`MongoCollection::save() <mongocollection.save>` and
231
- :php:`MongoCollection::findAndModify() <mongocollection.findandmodify>`
232
- have different modes of operation, depending on their arguments. Methods were
233
- also split to distinguish between :manual:`updating specific fields
234
- </tutorial/modify-documents>` and :manual:`full-document replacement
235
- </tutorial/modify-documents/#replace-the-document>`.
236
-
237
- Group Command Helper
238
- --------------------
239
-
240
- :phpclass:`MongoDB\\Collection` does have a helper method for the
241
- :manual:`group </reference/command/group>` command; The following example
242
- demonstrates how to execute a group command using the
243
- :phpmethod:`MongoDB\\Database::command()` method:
244
-
245
- .. code-block:: php
246
-
247
- <?php
248
-
249
- $database = (new MongoDB\Client)->selectDatabase('db_name');
250
- $cursor = $database->command([
251
- 'group' => [
252
- 'ns' => 'collection_name',
253
- 'key' => ['field_name' => 1],
254
- 'initial' => ['total' => 0],
255
- '$reduce' => new MongoDB\BSON\Javascript('...'),
256
- ],
257
- ]);
258
-
259
- $resultDocument = $cursor->toArray()[0];
260
-
261
- DBRef Helpers
262
- -------------
263
-
264
- :phpclass:`MongoDB\\Collection` does not yet have helper methods for working
265
- with :manual:`DBRef </reference/database-references>` objects; however, that is
266
- planned in :issue:`PHPLIB-24`.
267
-
268
- MongoCollection::save() Removed
269
- -------------------------------
270
-
271
- :php:`MongoCollection::save() <mongocollection.save>`, which was syntactic sugar
272
- for an insert or upsert operation, has been removed in favor of explicitly using
273
- :phpmethod:`MongoDB\\Collection::insertOne` or
274
- :phpmethod:`MongoDB\\Collection::replaceOne` (with the ``upsert`` option).
275
-
276
- .. .. figure:: /images/save-flowchart.png
277
- .. :alt: save() flowchart
278
-
279
- While the ``save`` method does have its uses for interactive environments, such
280
- as the mongo shell, it was intentionally excluded from the `CRUD specification
281
- <https://github.com/mongodb/specifications/blob/master/source/crud/crud.rst>`_
282
- for language drivers. Generally, application code should know if the document
283
- has an identifier and be able to explicitly insert or replace the document and
284
- handle the returned :phpclass:`MongoDB\\InsertOneResult` or
285
- :phpclass:`MongoDB\\UpdateResult`, respectively. This also helps avoid
286
- inadvertent and potentially dangerous :manual:`full-document replacements
287
- </tutorial/modify-documents>`.
288
-
289
238
Accessing IDs of Inserted Documents
290
239
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
291
240
@@ -310,10 +259,52 @@ following methods on the write result objects:
310
259
:phpmethod:`MongoDB\\Collection::bulkWrite()`
311
260
312
261
Bulk Write Operations
313
- ---------------------
262
+ ~~~~~~~~~~~~~~~~~~~~~
314
263
315
264
The legacy driver's :php:`MongoWriteBatch <class.mongowritebatch>` classes have
316
265
been replaced with a general-purpose
317
266
:phpmethod:`MongoDB\\Collection::bulkWrite()` method. Whereas the legacy driver
318
267
only allowed bulk operations of the same type, the new method allows operations
319
268
to be mixed (e.g. inserts, updates, and deletes).
269
+
270
+ MongoCollection::save() Removed
271
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
272
+
273
+ :php:`MongoCollection::save() <mongocollection.save>`, which was syntactic sugar
274
+ for an insert or upsert operation, has been removed in favor of explicitly using
275
+ :phpmethod:`MongoDB\\Collection::insertOne` or
276
+ :phpmethod:`MongoDB\\Collection::replaceOne` (with the ``upsert`` option).
277
+
278
+ While the ``save`` method does have its uses for interactive environments, such
279
+ as the ``mongo`` shell, it was intentionally excluded from the
280
+ `CRUD specification <https://github.com/mongodb/specifications/blob/master/source/crud/crud.rst>`_
281
+ for language drivers. Generally, application code should know if the document
282
+ has an identifier and be able to explicitly insert or replace the document and
283
+ handle the returned :phpclass:`MongoDB\\InsertOneResult` or
284
+ :phpclass:`MongoDB\\UpdateResult`, respectively. This also helps avoid
285
+ inadvertent and potentially dangerous :manual:`full-document replacements
286
+ </tutorial/modify-documents>`.
287
+
288
+ Group Command Helper
289
+ ~~~~~~~~~~~~~~~~~~~~
290
+
291
+ :phpclass:`MongoDB\\Collection` does have a helper method for the
292
+ :manual:`group </reference/command/group>` command. The following example
293
+ demonstrates how to execute a group command using the
294
+ :phpmethod:`MongoDB\\Database::command()` method:
295
+
296
+ .. code-block:: php
297
+
298
+ <?php
299
+
300
+ $database = (new MongoDB\Client)->selectDatabase('db_name');
301
+ $cursor = $database->command([
302
+ 'group' => [
303
+ 'ns' => 'collection_name',
304
+ 'key' => ['field_name' => 1],
305
+ 'initial' => ['total' => 0],
306
+ '$reduce' => new MongoDB\BSON\Javascript('...'),
307
+ ],
308
+ ]);
309
+
310
+ $resultDocument = $cursor->toArray()[0];
0 commit comments