/**
* Ensure sharding for collection by document name.
*
* @param string $documentName
* @param array $indexOptions Options for `ensureIndex` command. It's performed on an existing collections.
*
* @throws MongoDBException
*/
public function ensureDocumentSharding($documentName, array $indexOptions = array())
{
$class = $this->dm->getClassMetadata($documentName);
if (!$class->isSharded()) {
return;
}
$this->enableShardingForDbByDocumentName($documentName);
do {
$result = $this->runShardCollectionCommand($documentName);
$done = true;
$try = 0;
if ($result['ok'] != 1 && isset($result['proposedKey'])) {
$this->dm->getDocumentCollection($documentName)->ensureIndex($result['proposedKey'], $indexOptions);
$done = false;
$try++;
}
} while (!$done && $try < 2);
if ($result['ok'] != 1 && $result['errmsg'] !== 'already sharded') {
throw MongoDBException::failedToEnsureDocumentSharding($documentName, $result['errmsg']);
}
}