Skip to content

Commit 4daccd7

Browse files
committed
PHPLIB-45: Collection enumeration methods
1 parent 37edc90 commit 4daccd7

File tree

2 files changed

+82
-2
lines changed

2 files changed

+82
-2
lines changed

src/Database.php

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@
55
use MongoDB\Collection;
66
use MongoDB\Driver\Command;
77
use MongoDB\Driver\Manager;
8+
use MongoDB\Driver\Query;
89
use MongoDB\Driver\ReadPreference;
910
use MongoDB\Driver\Result;
1011
use MongoDB\Driver\WriteConcern;
12+
use MongoDB\Model\CollectionInfoIterator;
13+
use MongoDB\Model\CollectionInfoCommandIterator;
14+
use MongoDB\Model\CollectionInfoLegacyIterator;
15+
use InvalidArgumentException;
1116

1217
class Database
1318
{
@@ -84,11 +89,12 @@ public function dropCollection($collectionName)
8489
*
8590
* @see http://docs.mongodb.org/manual/reference/command/listCollections/
8691
* @param array $options
87-
* @return Result
92+
* @return CollectionInfoIterator
8893
*/
8994
public function listCollections(array $options = array())
9095
{
91-
// TODO
96+
// TODO: Determine if command or legacy method should be used
97+
return $this->listCollectionsCommand($options);
9298
}
9399

94100
/**
@@ -110,4 +116,58 @@ public function selectCollection($collectionName, WriteConcern $writeConcern = n
110116

111117
return new Collection($this->manager, $namespace, $writeConcern, $readPreference);
112118
}
119+
120+
/**
121+
* Returns information for all collections in this database using the
122+
* listCollections command.
123+
*
124+
* @param array $options
125+
* @return CollectionInfoCommandIterator
126+
*/
127+
private function listCollectionsCommand(array $options = array())
128+
{
129+
$command = new Command(array('listCollections' => 1) + $options);
130+
// TODO: Relax RP if connected to a secondary node in standalone mode
131+
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
132+
133+
$result = $this->manager->executeCommand($this->databaseName, $command, $readPreference);
134+
135+
return new CollectionInfoCommandIterator($result);
136+
}
137+
138+
/**
139+
* Returns information for all collections in this database by querying
140+
* the "system.namespaces" collection (MongoDB <2.8).
141+
*
142+
* @param array $options
143+
* @return CollectionInfoLegacyIterator
144+
* @throws InvalidArgumentException if the filter option is neither an array
145+
* nor object, or if filter.name is not a
146+
* string.
147+
*/
148+
private function listCollectionsLegacy(array $options = array())
149+
{
150+
$filter = array_key_exists('filter', $options) ? $options['filter'] : array();
151+
152+
if ( ! is_array($filter) && ! is_object($filter)) {
153+
throw new InvalidArgumentException(sprintf('Expected filter to be array or object, %s given', gettype($filter)));
154+
}
155+
156+
if (array_key_exists('name', $filter)) {
157+
if ( ! is_string($filter['name'])) {
158+
throw new InvalidArgumentException(sprintf('Filter "name" must be a string for MongoDB <2.8, %s given', gettype($filter['name'])));
159+
}
160+
161+
$filter['name'] = $this->databaseName . '.' . $filter['name'];
162+
}
163+
164+
$namespace = $this->databaseName . '.system.namespaces';
165+
$query = new Query($filter);
166+
// TODO: Relax RP if connected to a secondary node in standalone mode
167+
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
168+
169+
$result = $this->manager->executeQuery($namespace, $query, $readPreference);
170+
171+
return new CollectionInfoLegacyIterator($result);
172+
}
113173
}

tests/DatabaseFunctionalTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,24 @@ public function testDropCollection()
3939
$this->assertCommandSucceeded($commandResult);
4040
$this->assertCollectionCount($this->getNamespace(), 0);
4141
}
42+
43+
public function testListCollections()
44+
{
45+
$writeResult = $this->manager->executeInsert($this->getNamespace(), array('x' => 1));
46+
$this->assertEquals(1, $writeResult->getInsertedCount());
47+
48+
$collections = $this->database->listCollections();
49+
$this->assertInstanceOf('MongoDB\Model\CollectionInfoIterator', $collections);
50+
51+
$foundCollection = null;
52+
53+
foreach ($collections as $collection) {
54+
if ($collection->getName() === $this->getCollectionName()) {
55+
$foundCollection = $collection;
56+
break;
57+
}
58+
}
59+
60+
$this->assertNotNull($foundCollection, 'Found test collection in list of collection');
61+
}
4262
}

0 commit comments

Comments
 (0)