Skip to content

Commit af72fa5

Browse files
committed
PHPLIB-59: Implement insertMany() and InsertManyResult
1 parent 55cf25d commit af72fa5

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

src/Collection.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,34 @@ public function insertOne(array $document)
422422
return new InsertOneResult($wr, $id);
423423
}
424424

425+
/**
426+
* Inserts the provided documents
427+
*
428+
* @see http://docs.mongodb.org/manual/reference/command/insert/
429+
*
430+
* @param array $documents The documents to insert
431+
* @return InsertManyResult
432+
*/
433+
public function insertMany(array $documents)
434+
{
435+
$options = array_merge($this->getWriteOptions());
436+
437+
$bulk = new BulkWrite($options["ordered"]);
438+
$insertedIds = array();
439+
440+
foreach ($documents as $i => $document) {
441+
$insertedId = $bulk->insert($document);
442+
443+
if ($insertedId !== null) {
444+
$insertedIds[$i] = $insertedId;
445+
}
446+
}
447+
448+
$writeResult = $this->manager->executeBulkWrite($this->ns, $bulk, $this->wc);
449+
450+
return new InsertManyResult($writeResult, $insertedIds);
451+
}
452+
425453
/**
426454
* Internal helper for delete one/many documents
427455
* @internal

src/InsertManyResult.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
namespace MongoDB;
4+
5+
use MongoDB\Driver\WriteResult;
6+
7+
/**
8+
* Result class for a multi-document write operation.
9+
*/
10+
class InsertManyResult
11+
{
12+
private $writeResult;
13+
private $insertedIds;
14+
15+
/**
16+
* Constructor.
17+
*
18+
* @param WriteResult $writeResult
19+
* @param array $insertedIds
20+
*/
21+
public function __construct(WriteResult $writeResult, array $insertedIds = array())
22+
{
23+
$this->writeResult = $writeResult;
24+
$this->insertedIds = $insertedIds;
25+
}
26+
27+
/**
28+
* Return the number of documents that were inserted.
29+
*
30+
* This value is undefined if the write was not acknowledged.
31+
*
32+
* @see InsertManyResult::isAcknowledged()
33+
* @return integer
34+
*/
35+
public function getInsertedCount()
36+
{
37+
return $this->writeResult->getInsertedCount();
38+
}
39+
40+
/**
41+
* Return the map of inserted IDs that were generated by the driver.
42+
*
43+
* The index of each ID in the map corresponds to the document's position
44+
* in bulk operation. If an inserted document already had an ID (e.g. it was
45+
* generated by the application), it will not be present in this map.
46+
*
47+
* @return array
48+
*/
49+
public function getInsertedIds()
50+
{
51+
return $this->insertedIds;
52+
}
53+
54+
/**
55+
* Return whether this insert result was acknowledged by the server.
56+
*
57+
* If the insert was not acknowledged, other fields from the WriteResult
58+
* (e.g. insertedCount) will be undefined.
59+
*
60+
* @return boolean
61+
*/
62+
public function isAcknowledged()
63+
{
64+
return $this->writeResult->isAcknowledged();
65+
}
66+
}

0 commit comments

Comments
 (0)