Skip to content

Commit 1c5f2ea

Browse files
committed
PHPLIB-46: Index enumeration methods
1 parent 79d07ff commit 1c5f2ea

File tree

1 file changed

+48
-4
lines changed

1 file changed

+48
-4
lines changed

src/Collection.php

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
namespace MongoDB;
44

5+
use MongoDB\Driver\BulkWrite;
56
use MongoDB\Driver\Command;
67
use MongoDB\Driver\Cursor;
78
use MongoDB\Driver\Manager;
89
use MongoDB\Driver\Query;
910
use MongoDB\Driver\ReadPreference;
10-
use MongoDB\Driver\BulkWrite;
11+
use MongoDB\Driver\Server;
1112
use MongoDB\Driver\WriteConcern;
13+
use MongoDB\Model\IndexInfoIterator;
14+
use MongoDB\Model\IndexInfoIteratorIterator;
1215
use InvalidArgumentException;
1316

1417
class Collection
@@ -963,15 +966,23 @@ public function insertOne(array $document)
963966
}
964967

965968
/**
966-
* Returns information for all indexes in the collection.
969+
* Returns information for all indexes for the collection.
967970
*
968971
* @see http://docs.mongodb.org/manual/reference/command/listIndexes/
969972
* @see http://docs.mongodb.org/manual/reference/method/db.collection.getIndexes/
970-
* @return Cursor
973+
* @return IndexInfoIterator
971974
*/
972975
public function listIndexes()
973976
{
974-
// TODO
977+
$readPreference = new ReadPreference(ReadPreference::RP_PRIMARY);
978+
$server = $this->manager->selectServer($readPreference);
979+
980+
$serverInfo = $server->getInfo();
981+
$maxWireVersion = isset($serverInfo['maxWireVersion']) ? $serverInfo['maxWireVersion'] : 0;
982+
983+
return ($maxWireVersion >= 3)
984+
? $this->listIndexesCommand($server)
985+
: $this->listIndexesLegacy($server);
975986
}
976987

977988
/**
@@ -1150,4 +1161,37 @@ protected function _update($filter, $update, $options)
11501161
$bulk->update($filter, $update, $options);
11511162
return $this->manager->executeBulkWrite($this->ns, $bulk, $this->wc);
11521163
}
1164+
1165+
/**
1166+
* Returns information for all indexes for this collection using the
1167+
* listIndexes command.
1168+
*
1169+
* @see http://docs.mongodb.org/manual/reference/command/listIndexes/
1170+
* @param Server $server
1171+
* @return IndexInfoIteratorIterator
1172+
*/
1173+
private function listIndexesCommand(Server $server)
1174+
{
1175+
$command = new Command(array('listIndexes' => $this->collname));
1176+
$cursor = $server->executeCommand($this->dbname, $command);
1177+
$cursor->setTypeMap(array('document' => 'array'));
1178+
1179+
return new IndexInfoIteratorIterator($cursor);
1180+
}
1181+
1182+
/**
1183+
* Returns information for all indexes for this collection by querying the
1184+
* "system.indexes" collection (MongoDB <2.8).
1185+
*
1186+
* @param Server $server
1187+
* @return IndexInfoIteratorIterator
1188+
*/
1189+
private function listIndexesLegacy(Server $server)
1190+
{
1191+
$query = new Query(array('ns' => $this->ns));
1192+
$cursor = $server->executeQuery($namespace, $query);
1193+
$cursor->setTypeMap(array('document' => 'array'));
1194+
1195+
return new IndexInfoIteratorIterator($cursor);
1196+
}
11531197
}

0 commit comments

Comments
 (0)