Skip to content

Commit cfcf48f

Browse files
committed
Merge pull request #11
2 parents daed771 + 39376f3 commit cfcf48f

File tree

6 files changed

+398
-16
lines changed

6 files changed

+398
-16
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: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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
/**

src/InsertManyResult.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class InsertManyResult
1818
* @param WriteResult $writeResult
1919
* @param mixed[] $insertedIds
2020
*/
21-
public function __construct(WriteResult $writeResult, array $insertedIds = array())
21+
public function __construct(WriteResult $writeResult, array $insertedIds)
2222
{
2323
$this->writeResult = $writeResult;
2424
$this->insertedIds = $insertedIds;
@@ -38,11 +38,11 @@ public function getInsertedCount()
3838
}
3939

4040
/**
41-
* Return the map of inserted IDs that were generated by the driver.
41+
* Return a map of the inserted documents' IDs.
4242
*
4343
* The index of each ID in the map corresponds to the document's position
44-
* in bulk operation. If the document already an ID prior to insertion (i.e.
45-
* the driver did not need to generate an ID), this will contain its "_id".
44+
* in bulk operation. If the document had an ID prior to insertion (i.e. the
45+
* driver did not generate an ID), this will contain its "_id" field value.
4646
* Any driver-generated ID will be an MongoDB\Driver\ObjectID instance.
4747
*
4848
* @return mixed[]

src/InsertOneResult.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function getInsertedCount()
3838
}
3939

4040
/**
41-
* Return the inserted ID that was generated by the driver.
41+
* Return the inserted document's ID.
4242
*
4343
* If the document already an ID prior to insertion (i.e. the driver did not
4444
* need to generate an ID), this will contain its "_id". Any

src/UpdateResult.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace MongoDB;
44

5-
use BSON\ObjectId;
65
use MongoDB\Driver\WriteResult;
76

87
/**
@@ -38,7 +37,8 @@ public function getMatchedCount()
3837
/**
3938
* Return the number of documents that were modified.
4039
*
41-
* This value is undefined if the write was not acknowledged.
40+
* This value is undefined if the write was not acknowledged or if the write
41+
* executed as a legacy operation instead of write command.
4242
*
4343
* @see UpdateResult::isAcknowledged()
4444
* @return integer
@@ -48,12 +48,25 @@ public function getModifiedCount()
4848
return $this->writeResult->getModifiedCount();
4949
}
5050

51+
/**
52+
* Return the number of documents that were upserted.
53+
*
54+
* This value is undefined if the write was not acknowledged.
55+
*
56+
* @see UpdateResult::isAcknowledged()
57+
* @return integer
58+
*/
59+
public function getUpsertedCount()
60+
{
61+
return $this->writeResult->getUpsertedCount();
62+
}
63+
5164
/**
5265
* Return the ID of the document inserted by an upsert operation.
5366
*
5467
* This value is undefined if an upsert did not take place.
5568
*
56-
* @return ObjectId|null
69+
* @return mixed|null
5770
*/
5871
public function getUpsertedId()
5972
{

0 commit comments

Comments
 (0)