Skip to content

Commit e1d2621

Browse files
committed
PHPLIB-46: Index enumeration methods
1 parent 40fa724 commit e1d2621

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
@@ -964,15 +967,23 @@ public function insertOne(array $document)
964967
}
965968

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

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

0 commit comments

Comments
 (0)