Skip to content

Commit 72bf1b8

Browse files
committed
Revert change on modifyCollection
1 parent 260c266 commit 72bf1b8

File tree

6 files changed

+51
-43
lines changed

6 files changed

+51
-43
lines changed

UPGRADE-2.0.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ type changed to `void`. In case of an error, an exception is thrown.
3434
* `MongoDB\Database`: `createCollection`, `drop`, `dropCollection`, `modifyCollection`, `renameCollection`
3535
* `MongoDB\Database::createEncryptedCollection()` returns the list of encrypted fields
3636

37-
If you still need to access the raw command result, you can use a `CommandSubscriber`.
37+
If you still need to access the raw command result, you can use a
38+
[`CommandSubscriber`](https://www.php.net/manual/en/class.mongodb-driver-monitoring-commandsubscriber.php).
3839

3940
GridFS
4041
------

psalm-baseline.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,9 @@
733733
</MixedAssignment>
734734
</file>
735735
<file src="src/Operation/ModifyCollection.php">
736+
<MixedArgument>
737+
<code><![CDATA[$this->options['typeMap']]]></code>
738+
</MixedArgument>
736739
<MixedAssignment>
737740
<code><![CDATA[$cmd['comment']]]></code>
738741
<code><![CDATA[$options['session']]]></code>

src/Database.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -478,8 +478,12 @@ public function listCollections(array $options = []): Iterator
478478
* @throws InvalidArgumentException for parameter/option parsing errors
479479
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
480480
*/
481-
public function modifyCollection(string $collectionName, array $collectionOptions, array $options = []): void
481+
public function modifyCollection(string $collectionName, array $collectionOptions, array $options = []): array|object
482482
{
483+
if (! isset($options['typeMap'])) {
484+
$options['typeMap'] = $this->typeMap;
485+
}
486+
483487
$server = select_server_for_write($this->manager, $options);
484488

485489
if (! isset($options['writeConcern']) && ! is_in_transaction($options)) {
@@ -488,7 +492,7 @@ public function modifyCollection(string $collectionName, array $collectionOption
488492

489493
$operation = new ModifyCollection($this->databaseName, $collectionName, $collectionOptions, $options);
490494

491-
$operation->execute($server);
495+
return $operation->execute($server);
492496
}
493497

494498
/**

src/Operation/ModifyCollection.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
use MongoDB\Driver\WriteConcern;
2525
use MongoDB\Exception\InvalidArgumentException;
2626

27+
use function current;
28+
use function is_array;
29+
2730
/**
2831
* Operation for the collMod command.
2932
*
@@ -43,6 +46,9 @@ final class ModifyCollection
4346
*
4447
* * session (MongoDB\Driver\Session): Client session.
4548
*
49+
* * typeMap (array): Type map for BSON deserialization. This will only be
50+
* used for the returned command result document.
51+
*
4652
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
4753
*
4854
* @param string $databaseName Database name
@@ -61,6 +67,10 @@ public function __construct(private string $databaseName, private string $collec
6167
throw InvalidArgumentException::invalidType('"session" option', $this->options['session'], Session::class);
6268
}
6369

70+
if (isset($this->options['typeMap']) && ! is_array($this->options['typeMap'])) {
71+
throw InvalidArgumentException::invalidType('"typeMap" option', $this->options['typeMap'], 'array');
72+
}
73+
6474
if (isset($this->options['writeConcern']) && ! $this->options['writeConcern'] instanceof WriteConcern) {
6575
throw InvalidArgumentException::invalidType('"writeConcern" option', $this->options['writeConcern'], WriteConcern::class);
6676
}
@@ -73,11 +83,18 @@ public function __construct(private string $databaseName, private string $collec
7383
/**
7484
* Execute the operation.
7585
*
86+
* @return array|object Command result document
7687
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
7788
*/
78-
public function execute(Server $server): void
89+
public function execute(Server $server): array|object
7990
{
80-
$server->executeWriteCommand($this->databaseName, $this->createCommand(), $this->createOptions());
91+
$cursor = $server->executeWriteCommand($this->databaseName, $this->createCommand(), $this->createOptions());
92+
93+
if (isset($this->options['typeMap'])) {
94+
$cursor->setTypeMap($this->options['typeMap']);
95+
}
96+
97+
return current($cursor->toArray());
8198
}
8299

83100
private function createCommand(): Command

tests/Database/DatabaseFunctionalTest.php

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616
use PHPUnit\Framework\Attributes\Group;
1717
use TypeError;
1818

19+
use function array_key_exists;
1920
use function current;
20-
use function iterator_to_array;
21-
use function json_encode;
2221

2322
/**
2423
* Functional tests for the Database class.
@@ -182,25 +181,27 @@ public function testModifyCollection(): void
182181
$createIndexes = new CreateIndexes($this->getDatabaseName(), $this->getCollectionName(), $indexes);
183182
$createIndexes->execute($this->getPrimaryServer());
184183

185-
$this->database->modifyCollection(
184+
$commandResult = $this->database->modifyCollection(
186185
$this->getCollectionName(),
187186
['index' => ['keyPattern' => ['lastAccess' => 1], 'expireAfterSeconds' => 1000]],
187+
['typeMap' => ['root' => 'array', 'document' => 'array']],
188188
);
189-
190-
$indexes = $this->database->selectCollection($this->getCollectionName())->listIndexes();
191-
$indexes = iterator_to_array($indexes);
192-
$this->assertCount(2, $indexes);
193-
194-
foreach ($indexes as $index) {
195-
switch ($index['key']) {
196-
case ['_id' => 1]:
197-
break;
198-
case ['lastAccess' => 1]:
199-
$this->assertSame(1000, $index['expireAfterSeconds']);
200-
break;
201-
default:
202-
$this->fail('Unexpected index key: ' . json_encode($index->key));
189+
$this->assertCommandSucceeded($commandResult);
190+
$commandResult = (array) $commandResult;
191+
192+
if (array_key_exists('raw', $commandResult)) {
193+
/* Sharded environment, where we only assert if a shard had a successful update. For
194+
* non-primary shards that don't have chunks for the collection, the result contains a
195+
* "ns does not exist" error. */
196+
foreach ($commandResult['raw'] as $shard) {
197+
if (array_key_exists('ok', $shard) && $shard['ok'] == 1) {
198+
$this->assertSame(3, $shard['expireAfterSeconds_old']);
199+
$this->assertSame(1000, $shard['expireAfterSeconds_new']);
200+
}
203201
}
202+
} else {
203+
$this->assertSame(3, $commandResult['expireAfterSeconds_old']);
204+
$this->assertSame(1000, $commandResult['expireAfterSeconds_new']);
204205
}
205206
}
206207

tests/Operation/ModifyCollectionFunctionalTest.php

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@
33
namespace MongoDB\Tests\Operation;
44

55
use MongoDB\Operation\CreateIndexes;
6-
use MongoDB\Operation\ListIndexes;
76
use MongoDB\Operation\ModifyCollection;
87
use PHPUnit\Framework\Attributes\Group;
98

10-
use function iterator_to_array;
11-
use function json_encode;
12-
139
class ModifyCollectionFunctionalTest extends FunctionalTestCase
1410
{
1511
#[Group('matrix-testing-exclude-server-4.2-driver-4.0-topology-sharded_cluster')]
@@ -33,23 +29,9 @@ public function testCollMod(): void
3329
['index' => ['keyPattern' => ['lastAccess' => 1], 'expireAfterSeconds' => 1000]],
3430
['typeMap' => ['root' => 'array', 'document' => 'array']],
3531
);
36-
$modifyCollection->execute($this->getPrimaryServer());
37-
38-
$listIndexes = new ListIndexes($this->getDatabaseName(), $this->getCollectionName());
39-
$indexes = $listIndexes->execute($this->getPrimaryServer());
40-
$indexes = iterator_to_array($indexes);
41-
$this->assertCount(2, $indexes);
32+
$result = $modifyCollection->execute($this->getPrimaryServer());
4233

43-
foreach ($indexes as $index) {
44-
switch ($index['key']) {
45-
case ['_id' => 1]:
46-
break;
47-
case ['lastAccess' => 1]:
48-
$this->assertSame(1000, $index['expireAfterSeconds']);
49-
break;
50-
default:
51-
$this->fail('Unexpected index key: ' . json_encode($index->key));
52-
}
53-
}
34+
$this->assertSame(3, $result['expireAfterSeconds_old']);
35+
$this->assertSame(1000, $result['expireAfterSeconds_new']);
5436
}
5537
}

0 commit comments

Comments
 (0)