@@ -14,22 +14,20 @@ Overview
14
14
--------
15
15
16
16
The |php-library| and underlying :php:`mongodb extension <mongodb>` have notable
17
- API differences from the legacy :php:`mongo extension <mongo>`. This page will
18
- summarize those differences for the benefit of those upgrading from the legacy
19
- driver.
17
+ API differences from the legacy ``mongo`` extension. This page will summarize
18
+ those differences for the benefit of those upgrading from the legacy driver.
20
19
21
20
Additionally, a community-developed `mongo-php-adapter
22
21
<https://github.com/alcaeus/mongo-php-adapter>`_ library exists, which
23
- implements the `mongo extension <http://php.net/mongo>`_ API using this library
24
- and the new driver. While this adapter library is not officially supported by
25
- MongoDB, it does bear mentioning.
22
+ implements the `` mongo`` extension API using this library and the new driver.
23
+ While this adapter library is not officially supported by MongoDB, it does bear
24
+ mentioning.
26
25
27
26
BSON Type Classes
28
27
-----------------
29
28
30
- When upgrading from the legacy driver,
31
- `type classes <https://www.php.net/manual/en/mongo.types.php>`_ such as
32
- :php:`MongoId <mongoid>` must be replaced with classes in the
29
+ When upgrading from the legacy driver, type classes such as MongoId must be
30
+ replaced with classes in the
33
31
`MongoDB\\BSON namespace <https://www.php.net/manual/en/book.bson.php>`_. The
34
32
new driver also introduces interfaces for its BSON types, which should be
35
33
preferred if applications need to type hint against BSON values.
@@ -44,67 +42,66 @@ the new driver.
44
42
- BSON type class
45
43
- BSON type interface
46
44
47
- * - :php:` MongoId <mongoid>`
45
+ * - MongoId
48
46
- :php:`MongoDB\\BSON\\ObjectId <mongodb_bson_objectid>`
49
47
- :php:`MongoDB\\BSON\\ObjectIdInterface <mongodb_bson_objectidinterface>`
50
48
51
- * - :php:` MongoCode <mongocode>`
49
+ * - MongoCode
52
50
- :php:`MongoDB\\BSON\\Javascript <mongodb_bson_javascript>`
53
51
- :php:`MongoDB\\BSON\\JavascriptInterface <mongodb_bson_javascriptinterface>`
54
52
55
- * - :php:` MongoDate <mongodate>`
53
+ * - MongoDate
56
54
- :php:`MongoDB\\BSON\\UTCDateTime <mongodb_bson_utcdatetime>`
57
55
- :php:`MongoDB\\BSON\\UTCDateTimeInterface <mongodb_bson_utcdatetimeinterface>`
58
56
59
- * - :php:` MongoRegex <mongoregex>`
57
+ * - MongoRegex
60
58
- :php:`MongoDB\\BSON\\Regex <mongodb_bson_regex>`
61
59
- :php:`MongoDB\\BSON\\RegexInterface <mongodb_bson_regexinterface>`
62
60
63
- * - :php:` MongoBinData <mongobindata>`
61
+ * - MongoBinData
64
62
- :php:`MongoDB\\BSON\\Binary <mongodb_bson_binary>`
65
63
- :php:`MongoDB\\BSON\\BinaryInterface <mongodb_bson_binaryinterface>`
66
64
67
- * - :php:` MongoInt32 <mongoint32>`
65
+ * - MongoInt32
68
66
- Not implemented. [1]_
69
67
-
70
68
71
- * - :php:` MongoInt64 <mongoint64>`
69
+ * - MongoInt64
72
70
- :php:`MongoDB\\BSON\\Int64 <mongodb_bson_int64>`
73
71
- Not implemented. [2]_
74
72
75
- * - :php:` MongoDBRef <mongodbref>`
73
+ * - MongoDBRef
76
74
- Not implemented. [3]_
77
75
-
78
76
79
- * - :php:` MongoMinKey <mongominkey>`
77
+ * - MongoMinKey
80
78
- :php:`MongoDB\\BSON\\MinKey <mongodb_bson_minkey>`
81
79
- :php:`MongoDB\\BSON\\MinKeyInterface <mongodb_bson_minkeyinterface>`
82
80
83
- * - :php:` MongoMaxKey <mongomaxkey>`
81
+ * - MongoMaxKey
84
82
- :php:`MongoDB\\BSON\\MaxKey <mongodb_bson_maxkey>`
85
83
- :php:`MongoDB\\BSON\\MaxKeyInterface <mongodb_bson_maxkeyinterface>`
86
84
87
- * - :php:` MongoTimestamp <mongotimestamp>`
85
+ * - MongoTimestamp
88
86
- :php:`MongoDB\\BSON\\Timestamp <mongodb_bson_timestamp>`
89
87
- :php:`MongoDB\\BSON\\TimestampInterface <mongodb_bson_timestampinterface>`
90
88
91
- .. [1] The new driver does not implement an equivalent class for
92
- :php:`MongoInt32 <mongoint32>`. When decoding BSON, 32-bit integers will
93
- always be represented as a PHP integer. When encoding BSON, PHP integers will
94
- encode as either a 32-bit or 64-bit integer depending on their value.
89
+ .. [1] The new driver does not implement an equivalent class for MongoInt32.
90
+ When decoding BSON, 32-bit integers will always be represented as a PHP
91
+ integer. When encoding BSON, PHP integers will encode as either a 32-bit or
92
+ 64-bit integer depending on their value.
95
93
96
94
.. [2] :php:`MongoDB\\BSON\\Int64 <mongodb_bson_int64>` does not have an
97
95
interface defined. The new driver does not allow applications to instantiate
98
96
this type (i.e. its constructor is private) and it is only created during
99
97
BSON decoding when a 64-bit integer cannot be represented as a PHP integer on
100
98
a 32-bit platform.
101
99
102
- .. [3] The new driver does not implement an equivalent class for
103
- :php:`MongoDBRef <mongodbref>` since
104
- :manual:`DBRefs </reference/database-references>` are merely a BSON document
105
- with a particular structure and not a proper BSON type. The new driver also
106
- does not provide any helpers for working with DBRef objects, since their use
107
- is not encouraged.
100
+ .. [3] The new driver does not implement an equivalent class for MongoDBRef
101
+ since :manual:`DBRefs </reference/database-references>` are merely a BSON
102
+ document with a particular structure and not a proper BSON type. The new
103
+ driver also does not provide any helpers for working with DBRef objects,
104
+ since their use is not encouraged.
108
105
109
106
Collection API
110
107
--------------
@@ -116,15 +113,13 @@ and `Index Management
116
113
<https://github.com/mongodb/specifications/blob/master/source/index-management.rst>`_
117
114
specifications. Although some method names have changed in accordance with the
118
115
new specifications, the new class provides the same functionality as the legacy
119
- driver's :php:`MongoCollection <mongocollection>` class with some notable
120
- exceptions.
116
+ driver's MongoCollection class with some notable exceptions.
121
117
122
118
A guiding principle in designing the new APIs was that explicit method names are
123
119
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
120
+ ``MongoCollection::save()`` and ``MongoCollection::findAndModify()`` have
121
+ different modes of operation, depending on their arguments. Methods were also
122
+ split to distinguish between :manual:`updating specific fields
128
123
</tutorial/modify-documents>` and :manual:`full-document replacement
129
124
</tutorial/modify-documents/#replace-the-document>`.
130
125
@@ -134,116 +129,115 @@ equivalent method(s) in the new driver.
134
129
.. list-table::
135
130
:header-rows: 1
136
131
137
- * - :php:` MongoCollection <mongocollection>` method
132
+ * - MongoCollection method
138
133
- :phpclass:`MongoDB\\Collection` method(s)
139
134
140
- * - :php:` MongoCollection::aggregate() <mongocollection.aggregate> `
135
+ * - `` MongoCollection::aggregate()` `
141
136
- :phpmethod:`MongoDB\\Collection::aggregate()`
142
137
143
- * - :php:` MongoCollection::aggregateCursor() <mongocollection.aggregatecursor> `
138
+ * - `` MongoCollection::aggregateCursor()` `
144
139
- :phpmethod:`MongoDB\\Collection::aggregate()`
145
140
146
- * - :php:` MongoCollection::batchInsert() <mongocollection.batchinsert> `
141
+ * - `` MongoCollection::batchInsert()` `
147
142
- :phpmethod:`MongoDB\\Collection::insertMany()`
148
143
149
- * - :php:` MongoCollection::count() <mongocollection.count> `
144
+ * - `` MongoCollection::count()` `
150
145
- :phpmethod:`MongoDB\\Collection::count()`
151
146
152
- * - :php:` MongoCollection::createDBRef() <mongocollection.createdbref> `
147
+ * - `` MongoCollection::createDBRef()` `
153
148
- Not yet implemented. [3]_
154
149
155
- * - :php:` MongoCollection::createIndex() <mongocollection.createindex> `
150
+ * - `` MongoCollection::createIndex()` `
156
151
- :phpmethod:`MongoDB\\Collection::createIndex()`
157
152
158
- * - :php:` MongoCollection::deleteIndex() <mongocollection.deleteindex> `
153
+ * - `` MongoCollection::deleteIndex()` `
159
154
- :phpmethod:`MongoDB\\Collection::dropIndex()`
160
155
161
- * - :php:` MongoCollection::deleteIndexes() <mongocollection.deleteindexes> `
156
+ * - `` MongoCollection::deleteIndexes()` `
162
157
- :phpmethod:`MongoDB\\Collection::dropIndexes()`
163
158
164
- * - :php:` MongoCollection::drop() <mongocollection.drop> `
159
+ * - `` MongoCollection::drop()` `
165
160
- :phpmethod:`MongoDB\\Collection::drop()`
166
161
167
- * - :php:` MongoCollection::distinct() <mongocollection.distinct> `
162
+ * - `` MongoCollection::distinct()` `
168
163
- :phpmethod:`MongoDB\\Collection::distinct()`
169
164
170
- * - :php:` MongoCollection::ensureIndex() <mongocollection.ensureindex> `
165
+ * - `` MongoCollection::ensureIndex()` `
171
166
- :phpmethod:`MongoDB\\Collection::createIndex()`
172
167
173
- * - :php:` MongoCollection::find() <mongocollection.find> `
168
+ * - `` MongoCollection::find()` `
174
169
- :phpmethod:`MongoDB\\Collection::find()`
175
170
176
- * - :php:` MongoCollection::findAndModify() <mongocollection.findandmodify> `
171
+ * - `` MongoCollection::findAndModify()` `
177
172
- :phpmethod:`MongoDB\\Collection::findOneAndDelete()`,
178
173
:phpmethod:`MongoDB\\Collection::findOneAndReplace()`, and
179
174
:phpmethod:`MongoDB\\Collection::findOneAndUpdate()`
180
175
181
- * - :php:` MongoCollection::findOne() <mongocollection.findone> `
176
+ * - `` MongoCollection::findOne()` `
182
177
- :phpmethod:`MongoDB\\Collection::findOne()`
183
178
184
- * - :php:` MongoCollection::getDBRef() <mongocollection.getdbref> `
179
+ * - `` MongoCollection::getDBRef()` `
185
180
- Not implemented. [3]_
186
181
187
- * - :php:` MongoCollection::getIndexInfo() <mongocollection.getindexinfo> `
182
+ * - `` MongoCollection::getIndexInfo()` `
188
183
- :phpmethod:`MongoDB\\Collection::listIndexes()`
189
184
190
- * - :php:` MongoCollection::getName() <mongocollection.getname> `
185
+ * - `` MongoCollection::getName()` `
191
186
- :phpmethod:`MongoDB\\Collection::getCollectionName()`
192
187
193
- * - :php:` MongoCollection::getReadPreference() <mongocollection.getreadpreference> `
194
- - Not implemented.
188
+ * - `` MongoCollection::getReadPreference()` `
189
+ - :phpmethod:`MongoDB\\Collection::getReadPreference()`
195
190
196
- * - :php:` MongoCollection::getSlaveOkay() <mongocollection.getslaveokay> `
191
+ * - `` MongoCollection::getSlaveOkay()` `
197
192
- Not implemented.
198
193
199
- * - :php:` MongoCollection::getWriteConcern() <mongocollection.getwriteconcern> `
200
- - Not implemented.
194
+ * - `` MongoCollection::getWriteConcern()` `
195
+ - :phpmethod:`MongoDB\\Collection::getWriteConcern()`
201
196
202
- * - :php:` MongoCollection::group() <mongocollection.group> `
197
+ * - `` MongoCollection::group()` `
203
198
- Not implemented. Use :phpmethod:`MongoDB\\Database::command()`. See
204
199
`Group Command Helper`_ for an example.
205
200
206
- * - :php:` MongoCollection::insert() <mongocollection.insert> `
201
+ * - `` MongoCollection::insert()` `
207
202
- :phpmethod:`MongoDB\\Collection::insertOne()`
208
203
209
- * - :php:` MongoCollection::parallelCollectionScan() <mongocollection.parallelcollectionscan> `
204
+ * - `` MongoCollection::parallelCollectionScan()` `
210
205
- Not implemented.
211
206
212
- * - :php:` MongoCollection::remove() <mongocollection.remove> `
207
+ * - `` MongoCollection::remove()` `
213
208
- :phpmethod:`MongoDB\\Collection::deleteMany()` and
214
209
:phpmethod:`MongoDB\\Collection::deleteOne()`
215
210
216
- * - :php:` MongoCollection::save() <mongocollection.save> `
211
+ * - `` MongoCollection::save()` `
217
212
- :phpmethod:`MongoDB\\Collection::insertOne()` or
218
213
:phpmethod:`MongoDB\\Collection::replaceOne()` with the ``upsert``
219
214
option.
220
215
221
- * - :php:` MongoCollection::setReadPreference() <mongocollection.setreadpreference> `
216
+ * - `` MongoCollection::setReadPreference()` `
222
217
- Not implemented. Use :phpmethod:`MongoDB\\Collection::withOptions()`.
223
218
224
- * - :php:` MongoCollection::setSlaveOkay() <mongocollection.getslaveokay> `
219
+ * - `` MongoCollection::setSlaveOkay()` `
225
220
- Not implemented.
226
221
227
- * - :php:` MongoCollection::setWriteConcern() <mongocollection.setwriteconcern> `
222
+ * - `` MongoCollection::setWriteConcern()` `
228
223
- Not implemented. Use :phpmethod:`MongoDB\\Collection::withOptions()`.
229
224
230
- * - :php:` MongoCollection::update() <mongocollection.update> `
225
+ * - `` MongoCollection::update()` `
231
226
- :phpmethod:`MongoDB\\Collection::replaceOne()`,
232
227
:phpmethod:`MongoDB\\Collection::updateMany()`, and
233
228
:phpmethod:`MongoDB\\Collection::updateOne()`.
234
229
235
- * - :php:` MongoCollection::validate() <mongocollection.validate> `
230
+ * - `` MongoCollection::validate()` `
236
231
- Not implemented.
237
232
238
233
Accessing IDs of Inserted Documents
239
234
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
240
235
241
- In the legacy driver, :php:`MongoCollection::insert() <mongocollection.insert>`,
242
- :php:`MongoCollection::batchInsert() <mongocollection.batchinsert>`, and
243
- :php:`MongoCollection::save() <mongocollection.save>` (when inserting) would
244
- modify their input argument by injecting an ``_id`` key with a generated
245
- ObjectId (i.e. :php:`MongoId <class.mongoid>` object). This behavior was a bit
246
- of a hack, as it did not rely on the argument being :php:`passed by reference
236
+ In the legacy driver, ``MongoCollection::insert()``,
237
+ ``MongoCollection::batchInsert()``, and ``MongoCollection::save()`` (when
238
+ inserting) would modify their input argument by injecting an ``_id`` key with a
239
+ generated ObjectId (i.e. MongoId object). This behavior was a bit of a hack, as
240
+ it did not rely on the argument being :php:`passed by reference
247
241
<language.references.pass>`; instead, it directly modified memory through the
248
242
extension API and could not be implemented in PHP userland. As such, it is no
249
243
longer done in the new driver and library.
@@ -261,17 +255,16 @@ following methods on the write result objects:
261
255
Bulk Write Operations
262
256
~~~~~~~~~~~~~~~~~~~~~
263
257
264
- The legacy driver's :php:`MongoWriteBatch <class.mongowritebatch>` classes have
265
- been replaced with a general-purpose
266
- :phpmethod:`MongoDB\\Collection::bulkWrite()` method. Whereas the legacy driver
267
- only allowed bulk operations of the same type, the new method allows operations
268
- to be mixed (e.g. inserts, updates, and deletes).
258
+ The legacy driver's MongoWriteBatch classes have been replaced with a
259
+ general-purpose :phpmethod:`MongoDB\\Collection::bulkWrite()` method. Whereas
260
+ the legacy driver only allowed bulk operations of the same type, the new method
261
+ allows operations to be mixed (e.g. inserts, updates, and deletes).
269
262
270
263
MongoCollection::save() Removed
271
264
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
272
265
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
266
+ `` MongoCollection::save()`` , which was syntactic sugar for an insert or upsert
267
+ operation, has been removed in favor of explicitly using
275
268
:phpmethod:`MongoDB\\Collection::insertOne` or
276
269
:phpmethod:`MongoDB\\Collection::replaceOne` (with the ``upsert`` option).
277
270
0 commit comments