Skip to content

Commit 1497182

Browse files
committed
PHPLIB-94: BulkWriteResult and collect inserted IDs in bulkWrite()
1 parent bfd6861 commit 1497182

File tree

2 files changed

+149
-2
lines changed

2 files changed

+149
-2
lines changed

src/BulkWriteResult.php

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
3+
namespace MongoDB;
4+
5+
use BSON\ObjectId;
6+
use MongoDB\Driver\WriteResult;
7+
8+
/**
9+
* Result class for a bulk write operation.
10+
*/
11+
class BulkWriteResult
12+
{
13+
private $writeResult;
14+
private $insertedIds;
15+
16+
/**
17+
* Constructor.
18+
*
19+
* @param WriteResult $writeResult
20+
* @param mixed[] $insertedIds
21+
*/
22+
public function __construct(WriteResult $writeResult, array $insertedIds)
23+
{
24+
$this->writeResult = $writeResult;
25+
$this->insertedIds = $insertedIds;
26+
}
27+
28+
/**
29+
* Return the number of documents that were deleted.
30+
*
31+
* This value is undefined if the write was not acknowledged.
32+
*
33+
* @see BulkWriteResult::isAcknowledged()
34+
* @return integer
35+
*/
36+
public function getDeletedCount()
37+
{
38+
return $this->writeResult->getDeletedCount();
39+
}
40+
41+
/**
42+
* Return the number of documents that were inserted.
43+
*
44+
* This value is undefined if the write was not acknowledged.
45+
*
46+
* @see BulkWriteResult::isAcknowledged()
47+
* @return integer
48+
*/
49+
public function getInsertedCount()
50+
{
51+
return $this->writeResult->getInsertedCount();
52+
}
53+
54+
/**
55+
* Return a map of the inserted documents' IDs.
56+
*
57+
* The index of each ID in the map corresponds to the document's position
58+
* in bulk operation. If the document had an ID prior to insertion (i.e. the
59+
* driver did not generate an ID), this will contain its "_id" field value.
60+
* Any driver-generated ID will be an MongoDB\Driver\ObjectID instance.
61+
*
62+
* @return mixed[]
63+
*/
64+
public function getInsertedIds()
65+
{
66+
return $this->insertedIds;
67+
}
68+
69+
/**
70+
* Return the number of documents that were matched by the filter.
71+
*
72+
* This value is undefined if the write was not acknowledged.
73+
*
74+
* @see BulkWriteResult::isAcknowledged()
75+
* @return integer
76+
*/
77+
public function getMatchedCount()
78+
{
79+
return $this->writeResult->getMatchedCount();
80+
}
81+
82+
/**
83+
* Return the number of documents that were modified.
84+
*
85+
* This value is undefined if the write was not acknowledged or if the write
86+
* executed as a legacy operation instead of write command.
87+
*
88+
* @see BulkWriteResult::isAcknowledged()
89+
* @return integer|null
90+
*/
91+
public function getModifiedCount()
92+
{
93+
return $this->writeResult->getModifiedCount();
94+
}
95+
96+
/**
97+
* Return the number of documents that were upserted.
98+
*
99+
* This value is undefined if the write was not acknowledged.
100+
*
101+
* @see BulkWriteResult::isAcknowledged()
102+
* @return integer
103+
*/
104+
public function getUpsertedCount()
105+
{
106+
return $this->writeResult->getUpsertedCount();
107+
}
108+
109+
/**
110+
* Return a map of the upserted documents' IDs.
111+
*
112+
* The index of each ID in the map corresponds to the document's position
113+
* in bulk operation. If the document had an ID prior to upserting (i.e. the
114+
* server did not need to generate an ID), this will contain its "_id". Any
115+
* server-generated ID will be an MongoDB\Driver\ObjectID instance.
116+
*
117+
* @return mixed[]
118+
*/
119+
public function getUpsertedIds()
120+
{
121+
return $this->writeResult->getUpsertedIds();
122+
}
123+
124+
/**
125+
* Return whether this update was acknowledged by the server.
126+
*
127+
* If the update was not acknowledged, other fields from the WriteResult
128+
* (e.g. matchedCount) will be undefined.
129+
*
130+
* @return boolean
131+
*/
132+
public function isAcknowledged()
133+
{
134+
return $this->writeResult->isAcknowledged();
135+
}
136+
}

src/Collection.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ public function bulkWrite(array $ops, array $options = array())
207207
$options = array_merge($this->getBulkOptions(), $options);
208208

209209
$bulk = new BulkWrite($options["ordered"]);
210+
$insertedIds = array();
210211

211212
foreach ($ops as $n => $op) {
212213
foreach ($op as $opname => $args) {
@@ -216,7 +217,14 @@ public function bulkWrite(array $ops, array $options = array())
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":
@@ -269,7 +277,10 @@ public function bulkWrite(array $ops, array $options = array())
269277
}
270278
}
271279
}
272-
return $this->manager->executeBulkWrite($this->ns, $bulk, $this->wc);
280+
281+
$writeResult = $this->manager->executeBulkWrite($this->ns, $bulk, $this->wc);
282+
283+
return new BulkWriteResult($writeResult, $insertedIds);
273284
}
274285

275286
/**

0 commit comments

Comments
 (0)