@@ -83,7 +83,9 @@ between [updating specific fields][update] and
83
83
[ MongoDB\Collection] [ collection ] does not yet have a helper method for the
84
84
[ group] [ group ] command; however, that is planned in [ PHPLIB-177] [ jira-group ] .
85
85
The following example demonstrates how to execute a group command using
86
- [ Database::command()] ( classes/database.md#command ) :
86
+ [ Database::command()] [ command ] :
87
+
88
+ [ command ] : classes/database.md#command
87
89
88
90
``` php
89
91
<?php
@@ -108,7 +110,7 @@ $resultDocument = $cursor->toArray()[0];
108
110
[ MongoDB\Collection] [ collection ] does not yet have a helper method for the
109
111
[ mapReduce] [ mapReduce ] command; however, that is planned in
110
112
[ PHPLIB-53] [ jira-mapreduce ] . The following example demonstrates how to execute a
111
- mapReduce command using [ Database::command()] ( classes/database.md# command) :
113
+ mapReduce command using [ Database::command()] [ command ] :
112
114
113
115
``` php
114
116
<?php
@@ -139,8 +141,11 @@ with [DBRef][dbref] objects; however, that is planned in
139
141
140
142
[ MongoCollection::save()] [ save ] , which was syntactic sugar for an insert or
141
143
upsert operation, has been removed in favor of explicitly using
142
- [ insertOne()] ( classes/collection.md#insertone ) or
143
- [ replaceOne()] ( classes/collection.md#replaceone ) (with the "upsert" option).
144
+ [ insertOne()] [ insertone ] or [ replaceOne()] [ replaceone ] (with the "upsert"
145
+ option).
146
+
147
+ [ insertone ] : classes/collection.md#insertone
148
+ [ replaceone ] : classes/collection.md#replaceone
144
149
145
150
![ save() flowchart] ( img/save-flowchart.png )
146
151
@@ -152,11 +157,37 @@ insert or replace the document and handle the returned InsertResult or
152
157
UpdateResult, respectively. This also helps avoid inadvertent and potentially
153
158
dangerous [ full-document replacements] [ replace ] .
154
159
160
+ ### Accessing IDs of Inserted Documents
161
+
162
+ In the legacy driver, [ MongoCollection::insert()] [ insert ] ,
163
+ [ MongoCollection::batchInsert()] [ batchinsert ] , and
164
+ [ MongoCollection::save()] [ save ] (when inserting) would modify their input
165
+ argument by injecting an "_ id" key containing the generated ObjectId (i.e.
166
+ [ MongoId] [ mongoid ] object). This behavior was a bit of a hack, as it did not
167
+ rely on the argument being [ passed by reference] [ byref ] ; it directly modified
168
+ memory through the extension API and could not be implemented in PHP userland.
169
+ As such, it is no longer done in the new driver and library.
170
+
171
+ [ insert ] : http://php.net/manual/en/mongocollection.insert.php
172
+ [ batchinsert ] : http://php.net/manual/en/mongocollection.batchinsert.php
173
+ [ mongoid ] : http://php.net/manual/en/class.mongoid.php
174
+ [ byref ] : http://php.net/manual/en/language.references.pass.php
175
+
176
+ IDs of inserted documents (whether generated or not) may be accessed through the
177
+ result objects returned by the write methods:
178
+
179
+ * MongoDB\InsertOneResult::getInsertedId() for [ insertOne()] [ insertone ]
180
+ * MongoDB\InsertManyResult::getInsertedIds() for [ insertMany()] [ insertmany ]
181
+ * MongoDB\BulkWriteResult::getInsertedIds() for [ bulkWrite()] [ bulkwrite ]
182
+
183
+ [ insertmany ] : classes/collection.md#insertmany
184
+ [ bulkwrite ] : classes/collection.md#bulkwrite
185
+
155
186
### MongoWriteBatch
156
187
157
188
The legacy driver's [ MongoWriteBatch] [ batch ] classes have been replaced with a
158
- general-purpose [ bulkWrite()] ( classes/collection.md# bulkwrite) method. Whereas
159
- the legacy driver only allowed bulk operations of the same time, the new method
160
- allows operations to be mixed (e.g. inserts, updates, and deletes).
189
+ general-purpose [ bulkWrite()] [ bulkwrite ] method. Whereas the legacy driver only
190
+ allowed bulk operations of the same time, the new method allows operations to be
191
+ mixed (e.g. inserts, updates, and deletes).
161
192
162
193
[ batch ] : http://php.net/manual/en/class.mongowritebatch.php
0 commit comments