Skip to content

Commit f66f430

Browse files
committed
Discuss how inserted IDs may be accessed in Upgrade Guide
1 parent 2c98016 commit f66f430

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

docs/upgrade-guide.md

+26-1
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,32 @@ insert or replace the document and handle the returned InsertResult or
157157
UpdateResult, respectively. This also helps avoid inadvertent and potentially
158158
dangerous [full-document replacements][replace].
159159

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+
160186
### MongoWriteBatch
161187

162188
The legacy driver's [MongoWriteBatch][batch] classes have been replaced with a
@@ -165,4 +191,3 @@ allowed bulk operations of the same time, the new method allows operations to be
165191
mixed (e.g. inserts, updates, and deletes).
166192

167193
[batch]: http://php.net/manual/en/class.mongowritebatch.php
168-
[bulkwrite]: classes/collection.md#bulkwrite

0 commit comments

Comments
 (0)