/**
* Executes a batch of write operations
*
* @see http://php.net/manual/en/mongowritebatch.execute.php
* @param array $writeOptions
* @return array
*/
public final function execute(array $writeOptions = [])
{
$writeOptions += $this->writeOptions;
if (!count($this->items)) {
return ['ok' => true];
}
if (isset($writeOptions['j'])) {
trigger_error('j parameter is not supported', E_WARNING);
}
if (isset($writeOptions['fsync'])) {
trigger_error('fsync parameter is not supported', E_WARNING);
}
$options['writeConcern'] = $this->createWriteConcernFromArray($writeOptions);
if (isset($writeOptions['ordered'])) {
$options['ordered'] = $writeOptions['ordered'];
}
$collection = $this->collection->getCollection();
try {
$result = $collection->BulkWrite($this->items, $options);
$ok = 1.0;
} catch (\MongoDB\Driver\Exception\BulkWriteException $e) {
$result = $e->getWriteResult();
$ok = 0.0;
}
if ($ok === 1.0) {
$this->items = [];
}
return ['ok' => $ok, 'nInserted' => $result->getInsertedCount(), 'nMatched' => $result->getMatchedCount(), 'nModified' => $result->getModifiedCount(), 'nUpserted' => $result->getUpsertedCount(), 'nRemoved' => $result->getDeletedCount()];
}