|
2 | 2 |
|
3 | 3 | namespace MongoDB;
|
4 | 4 |
|
| 5 | +use MongoDB\Driver\BulkWrite; |
5 | 6 | use MongoDB\Driver\Command;
|
6 | 7 | use MongoDB\Driver\Cursor;
|
7 | 8 | use MongoDB\Driver\Manager;
|
8 | 9 | use MongoDB\Driver\Query;
|
9 | 10 | use MongoDB\Driver\ReadPreference;
|
10 |
| -use MongoDB\Driver\BulkWrite; |
| 11 | +use MongoDB\Driver\Server; |
11 | 12 | use MongoDB\Driver\WriteConcern;
|
| 13 | +use MongoDB\Model\IndexInfoIterator; |
| 14 | +use MongoDB\Model\IndexInfoIteratorIterator; |
12 | 15 | use InvalidArgumentException;
|
13 | 16 |
|
14 | 17 | class Collection
|
@@ -964,15 +967,23 @@ public function insertOne(array $document)
|
964 | 967 | }
|
965 | 968 |
|
966 | 969 | /**
|
967 |
| - * Returns information for all indexes in the collection. |
| 970 | + * Returns information for all indexes for the collection. |
968 | 971 | *
|
969 | 972 | * @see http://docs.mongodb.org/manual/reference/command/listIndexes/
|
970 | 973 | * @see http://docs.mongodb.org/manual/reference/method/db.collection.getIndexes/
|
971 |
| - * @return Cursor |
| 974 | + * @return IndexInfoIterator |
972 | 975 | */
|
973 | 976 | public function listIndexes()
|
974 | 977 | {
|
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); |
976 | 987 | }
|
977 | 988 |
|
978 | 989 | /**
|
@@ -1151,4 +1162,37 @@ protected function _update($filter, $update, $options)
|
1151 | 1162 | $bulk->update($filter, $update, $options);
|
1152 | 1163 | return $this->manager->executeBulkWrite($this->ns, $bulk, $this->wc);
|
1153 | 1164 | }
|
| 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 | + } |
1154 | 1198 | }
|
0 commit comments