public function updateDocumentIndexes($documentName, $timeout = null)
{
$class = $this->dm->getClassMetadata($documentName);
if ($class->isMappedSuperclass || $class->isEmbeddedDocument || $class->isQueryResultDocument) {
throw new \InvalidArgumentException('Cannot update document indexes for mapped super classes, embedded documents or aggregation result documents.');
}
$documentIndexes = $this->getDocumentIndexes($documentName);
$collection = $this->dm->getDocumentCollection($documentName);
$mongoIndexes = $collection->getIndexInfo();
/* Determine which Mongo indexes should be deleted. Exclude the ID index
* and those that are equivalent to any in the class metadata.
*/
$self = $this;
$mongoIndexes = array_filter($mongoIndexes, function ($mongoIndex) use($documentIndexes, $self) {
if ('_id_' === $mongoIndex['name']) {
return false;
}
foreach ($documentIndexes as $documentIndex) {
if ($self->isMongoIndexEquivalentToDocumentIndex($mongoIndex, $documentIndex)) {
return false;
}
}
return true;
});
// Delete indexes that do not exist in class metadata
foreach ($mongoIndexes as $mongoIndex) {
if (isset($mongoIndex['name'])) {
/* Note: MongoCollection::deleteIndex() cannot delete
* custom-named indexes, so use the deleteIndexes command.
*/
$collection->getDatabase()->command(array('deleteIndexes' => $collection->getName(), 'index' => $mongoIndex['name']));
}
}
$this->ensureDocumentIndexes($documentName, $timeout);
}