Skip to content

Commit 9a56bda

Browse files
committed
Merge pull request #686
2 parents 09116d5 + 5fdf482 commit 9a56bda

File tree

2 files changed

+63
-72
lines changed

2 files changed

+63
-72
lines changed

docs/images/save-flowchart.png

-46.7 KB
Binary file not shown.

docs/upgrade.txt

Lines changed: 63 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Upgrade Guide
77
.. contents:: On this page
88
:local:
99
:backlinks: none
10-
:depth: 1
10+
:depth: 2
1111
:class: singlecol
1212

1313
Overview
@@ -119,14 +119,23 @@ new specifications, the new class provides the same functionality as the legacy
119119
driver's :php:`MongoCollection <mongocollection>` class with some notable
120120
exceptions.
121121

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.
124133

125134
.. list-table::
126135
:header-rows: 1
127136

128-
* - :php:`MongoCollection <mongocollection>`
129-
- :phpclass:`MongoDB\\Collection`
137+
* - :php:`MongoCollection <mongocollection>` method
138+
- :phpclass:`MongoDB\\Collection` method(s)
130139

131140
* - :php:`MongoCollection::aggregate() <mongocollection.aggregate>`
132141
- :phpmethod:`MongoDB\\Collection::aggregate()`
@@ -141,7 +150,7 @@ Old and New Methods
141150
- :phpmethod:`MongoDB\\Collection::count()`
142151

143152
* - :php:`MongoCollection::createDBRef() <mongocollection.createdbref>`
144-
- Not yet implemented. See :issue:`PHPLIB-24`.
153+
- Not yet implemented. [3]_
145154

146155
* - :php:`MongoCollection::createIndex() <mongocollection.createindex>`
147156
- :phpmethod:`MongoDB\\Collection::createIndex()`
@@ -173,7 +182,7 @@ Old and New Methods
173182
- :phpmethod:`MongoDB\\Collection::findOne()`
174183

175184
* - :php:`MongoCollection::getDBRef() <mongocollection.getdbref>`
176-
- Not implemented. See :issue:`PHPLIB-24`.
185+
- Not implemented. [3]_
177186

178187
* - :php:`MongoCollection::getIndexInfo() <mongocollection.getindexinfo>`
179188
- :phpmethod:`MongoDB\\Collection::listIndexes()`
@@ -191,7 +200,8 @@ Old and New Methods
191200
- Not implemented.
192201

193202
* - :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.
195205

196206
* - :php:`MongoCollection::insert() <mongocollection.insert>`
197207
- :phpmethod:`MongoDB\\Collection::insertOne()`
@@ -209,13 +219,13 @@ Old and New Methods
209219
option.
210220

211221
* - :php:`MongoCollection::setReadPreference() <mongocollection.setreadpreference>`
212-
- Not implemented. Use :phpmethod:`MongoDB\\Collection::withOptions()`
222+
- Not implemented. Use :phpmethod:`MongoDB\\Collection::withOptions()`.
213223

214224
* - :php:`MongoCollection::setSlaveOkay() <mongocollection.getslaveokay>`
215225
- Not implemented.
216226

217227
* - :php:`MongoCollection::setWriteConcern() <mongocollection.setwriteconcern>`
218-
- Not implemented. Use :phpmethod:`MongoDB\\Collection::withOptions()`
228+
- Not implemented. Use :phpmethod:`MongoDB\\Collection::withOptions()`.
219229

220230
* - :php:`MongoCollection::update() <mongocollection.update>`
221231
- :phpmethod:`MongoDB\\Collection::replaceOne()`,
@@ -225,67 +235,6 @@ Old and New Methods
225235
* - :php:`MongoCollection::validate() <mongocollection.validate>`
226236
- Not implemented.
227237

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-
289238
Accessing IDs of Inserted Documents
290239
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
291240

@@ -310,10 +259,52 @@ following methods on the write result objects:
310259
:phpmethod:`MongoDB\\Collection::bulkWrite()`
311260

312261
Bulk Write Operations
313-
---------------------
262+
~~~~~~~~~~~~~~~~~~~~~
314263

315264
The legacy driver's :php:`MongoWriteBatch <class.mongowritebatch>` classes have
316265
been replaced with a general-purpose
317266
:phpmethod:`MongoDB\\Collection::bulkWrite()` method. Whereas the legacy driver
318267
only allowed bulk operations of the same type, the new method allows operations
319268
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

Comments
 (0)