Skip to content

Commit 70f8c0a

Browse files
committed
Merge pull request #485
2 parents d9c277a + 0b511a0 commit 70f8c0a

6 files changed

+84
-1
lines changed

docs/includes/apiargs-MongoDBClient-method-listDatabases-option.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
1+
arg_name: option
2+
name: filter
3+
type: array|object
4+
description: |
5+
A query expression to filter the list of databases.
6+
7+
You can specify a query expression for database fields (e.g. ``name``,
8+
``sizeOnDisk``, ``empty``).
9+
10+
.. versionadded:: 1.3
11+
interface: phpmethod
12+
operation: ~
13+
optional: true
14+
---
115
source:
216
file: apiargs-common-option.yaml
317
ref: maxTimeMS

docs/includes/apiargs-MongoDBDatabase-method-listCollections-option.yaml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ type: array|object
44
description: |
55
A query expression to filter the list of collections.
66
7-
You can specify a query expression on the collection ``name`` and ``options``.
7+
You can specify a query expression for collection fields (e.g. ``name``,
8+
``options``).
9+
10+
For server versions < 3.0, the filter can only be used to match the ``name``
11+
field with a string value. More complex filters will result in an exception at
12+
execution time if used.
813
interface: phpmethod
914
operation: ~
1015
optional: true

src/Operation/ListCollections.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ class ListCollections implements Executable
4949
*
5050
* * filter (document): Query by which to filter collections.
5151
*
52+
* For server versions < 3.0, the filter can only be used to match the
53+
* "name" field with a string value. More complex filters will result in
54+
* an exception at execution time if used.
55+
*
5256
* * maxTimeMS (integer): The maximum amount of time to allow the query to
5357
* run.
5458
*

src/Operation/ListDatabases.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ class ListDatabases implements Executable
4242
*
4343
* Supported options:
4444
*
45+
* * filter (document): Query by which to filter databases.
46+
*
47+
* For servers < 3.6, this option is ignored.
48+
*
4549
* * maxTimeMS (integer): The maximum amount of time to allow the query to
4650
* run.
4751
*
@@ -54,6 +58,10 @@ class ListDatabases implements Executable
5458
*/
5559
public function __construct(array $options = [])
5660
{
61+
if (isset($options['filter']) && ! is_array($options['filter']) && ! is_object($options['filter'])) {
62+
throw InvalidArgumentException::invalidType('"filter" option', $options['filter'], 'array or object');
63+
}
64+
5765
if (isset($options['maxTimeMS']) && ! is_integer($options['maxTimeMS'])) {
5866
throw InvalidArgumentException::invalidType('"maxTimeMS" option', $options['maxTimeMS'], 'integer');
5967
}
@@ -78,6 +86,10 @@ public function execute(Server $server)
7886
{
7987
$cmd = ['listDatabases' => 1];
8088

89+
if ( ! empty($this->options['filter'])) {
90+
$cmd['filter'] = (object) $this->options['filter'];
91+
}
92+
8193
if (isset($this->options['maxTimeMS'])) {
8294
$cmd['maxTimeMS'] = $this->options['maxTimeMS'];
8395
}

tests/Operation/ListDatabasesFunctionalTest.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,56 @@
22

33
namespace MongoDB\Tests\Operation;
44

5+
use MongoDB\Operation\InsertOne;
56
use MongoDB\Operation\ListDatabases;
67
use MongoDB\Tests\CommandObserver;
78
use stdClass;
89

910
class ListDatabasesFunctionalTest extends FunctionalTestCase
1011
{
12+
public function testListDatabases()
13+
{
14+
$server = $this->getPrimaryServer();
15+
16+
$insertOne = new InsertOne($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1]);
17+
$writeResult = $insertOne->execute($server);
18+
$this->assertEquals(1, $writeResult->getInsertedCount());
19+
20+
$operation = new ListDatabases();
21+
$databases = $operation->execute($server);
22+
23+
$this->assertInstanceOf('MongoDB\Model\DatabaseInfoIterator', $databases);
24+
25+
foreach ($databases as $database) {
26+
$this->assertInstanceOf('MongoDB\Model\DatabaseInfo', $database);
27+
}
28+
}
29+
30+
public function testFilterOption()
31+
{
32+
if (version_compare($this->getServerVersion(), '3.6.0', '<')) {
33+
$this->markTestSkipped('listDatabase command "filter" option is not supported');
34+
}
35+
36+
$server = $this->getPrimaryServer();
37+
38+
$insertOne = new InsertOne($this->getDatabaseName(), $this->getCollectionName(), ['x' => 1]);
39+
$writeResult = $insertOne->execute($server);
40+
$this->assertEquals(1, $writeResult->getInsertedCount());
41+
42+
$operation = new ListDatabases(['filter' => ['name' => $this->getDatabaseName()]]);
43+
$databases = $operation->execute($server);
44+
45+
$this->assertInstanceOf('MongoDB\Model\DatabaseInfoIterator', $databases);
46+
47+
$this->assertCount(1, $databases);
48+
49+
foreach ($databases as $database) {
50+
$this->assertInstanceOf('MongoDB\Model\DatabaseInfo', $database);
51+
$this->assertEquals($this->getDatabaseName(), $database->getName());
52+
}
53+
}
54+
1155
public function testSessionOption()
1256
{
1357
if (version_compare($this->getServerVersion(), '3.6.0', '<')) {

tests/Operation/ListDatabasesTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public function provideInvalidConstructorOptions()
1919
{
2020
$options = [];
2121

22+
foreach ($this->getInvalidDocumentValues() as $value) {
23+
$options[][] = ['filter' => $value];
24+
}
25+
2226
foreach ($this->getInvalidIntegerValues() as $value) {
2327
$options[][] = ['maxTimeMS' => $value];
2428
}

0 commit comments

Comments
 (0)