@@ -198,32 +198,44 @@ function (\stdClass $document) { return (array) $document; },
198198 *
199199 * @see Collection::getBulkOptions() for supported $options
200200 *
201- * @param array $bulk Array of operations
201+ * @param array $ops Array of operations
202202 * @param array $options Additional options
203203 * @return WriteResult
204204 */
205- public function bulkWrite (array $ bulk , array $ options = array ())
205+ public function bulkWrite (array $ ops , array $ options = array ())
206206 {
207207 $ options = array_merge ($ this ->getBulkOptions (), $ options );
208208
209209 $ bulk = new BulkWrite ($ options ["ordered " ]);
210+ $ insertedIds = array ();
210211
211- foreach ($ bulk as $ n => $ op ) {
212+ foreach ($ ops as $ n => $ op ) {
212213 foreach ($ op as $ opname => $ args ) {
213214 if (!isset ($ args [0 ])) {
214215 throw new InvalidArgumentException (sprintf ("Missing argument#1 for '%s' (operation#%d) " , $ opname , $ n ));
215216 }
216217
217218 switch ($ opname ) {
218219 case "insertOne " :
219- $ bulk ->insert ($ args [0 ]);
220+ $ insertedId = $ bulk ->insert ($ args [0 ]);
221+
222+ if ($ insertedId !== null ) {
223+ $ insertedIds [$ n ] = $ insertedId ;
224+ } else {
225+ $ insertedIds [$ n ] = is_array ($ args [0 ]) ? $ args [0 ]['_id ' ] : $ args [0 ]->_id ;
226+ }
227+
220228 break ;
221229
222230 case "updateMany " :
223231 if (!isset ($ args [1 ])) {
224232 throw new InvalidArgumentException (sprintf ("Missing argument#2 for '%s' (operation#%d) " , $ opname , $ n ));
225233 }
226- $ options = array_merge ($ this ->getWriteOptions (), isset ($ args [2 ]) ? $ args [2 ] : array (), array ("limit " => 0 ));
234+ $ options = array_merge ($ this ->getWriteOptions (), isset ($ args [2 ]) ? $ args [2 ] : array (), array ("multi " => true ));
235+ $ firstKey = key ($ args [1 ]);
236+ if (!isset ($ firstKey [0 ]) || $ firstKey [0 ] != '$ ' ) {
237+ throw new InvalidArgumentException ("First key in \$update must be a \$operator " );
238+ }
227239
228240 $ bulk ->update ($ args [0 ], $ args [1 ], $ options );
229241 break ;
@@ -232,7 +244,7 @@ public function bulkWrite(array $bulk, array $options = array())
232244 if (!isset ($ args [1 ])) {
233245 throw new InvalidArgumentException (sprintf ("Missing argument#2 for '%s' (operation#%d) " , $ opname , $ n ));
234246 }
235- $ options = array_merge ($ this ->getWriteOptions (), isset ($ args [2 ]) ? $ args [2 ] : array (), array ("limit " => 1 ));
247+ $ options = array_merge ($ this ->getWriteOptions (), isset ($ args [2 ]) ? $ args [2 ] : array (), array ("multi " => false ));
236248 $ firstKey = key ($ args [1 ]);
237249 if (!isset ($ firstKey [0 ]) || $ firstKey [0 ] != '$ ' ) {
238250 throw new InvalidArgumentException ("First key in \$update must be a \$operator " );
@@ -245,7 +257,7 @@ public function bulkWrite(array $bulk, array $options = array())
245257 if (!isset ($ args [1 ])) {
246258 throw new InvalidArgumentException (sprintf ("Missing argument#2 for '%s' (operation#%d) " , $ opname , $ n ));
247259 }
248- $ options = array_merge ($ this ->getWriteOptions (), isset ($ args [2 ]) ? $ args [2 ] : array (), array ("limit " => 1 ));
260+ $ options = array_merge ($ this ->getWriteOptions (), isset ($ args [2 ]) ? $ args [2 ] : array (), array ("multi " => false ));
249261 $ firstKey = key ($ args [1 ]);
250262 if (isset ($ firstKey [0 ]) && $ firstKey [0 ] == '$ ' ) {
251263 throw new InvalidArgumentException ("First key in \$update must NOT be a \$operator " );
@@ -269,7 +281,10 @@ public function bulkWrite(array $bulk, array $options = array())
269281 }
270282 }
271283 }
272- return $ this ->manager ->executeBulkWrite ($ this ->ns , $ bulk , $ this ->wc );
284+
285+ $ writeResult = $ this ->manager ->executeBulkWrite ($ this ->ns , $ bulk , $ this ->wc );
286+
287+ return new BulkWriteResult ($ writeResult , $ insertedIds );
273288 }
274289
275290 /**
0 commit comments