isDefault()) { unset($options['writeConcern']); } $this->databaseName = (string) $databaseName; $this->collectionName = (string) $collectionName; $this->filter = $filter; $this->limit = $limit; $this->options = $options; } /** * Execute the operation. * * @see Executable::execute() * @param Server $server * @return DeleteResult * @throws DriverRuntimeException for other driver errors (e.g. connection errors) */ public function execute(Server $server) { if (isset($this->options['collation']) && ! \MongoDB\server_supports_feature($server, self::$wireVersionForCollation)) { throw UnsupportedException::collationNotSupported(); } $bulk = new Bulk(); $bulk->delete($this->filter, $this->createDeleteOptions()); $writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $this->createExecuteOptions()); return new DeleteResult($writeResult); } public function getCommandDocument(Server $server) { $cmd = ['delete' => $this->collectionName, 'deletes' => [['q' => $this->filter] + $this->createDeleteOptions()]]; if (isset($this->options['writeConcern'])) { $cmd['writeConcern'] = $this->options['writeConcern']; } return $cmd; } /** * Create options for the delete command. * * Note that these options are different from the bulk write options, which * are created in createExecuteOptions(). * * @return array */ private function createDeleteOptions() { $deleteOptions = ['limit' => $this->limit]; if (isset($this->options['collation'])) { $deleteOptions['collation'] = (object) $this->options['collation']; } return $deleteOptions; } /** * Create options for executing the bulk write. * * @see http://php.net/manual/en/mongodb-driver-server.executebulkwrite.php * @return array */ private function createExecuteOptions() { $options = []; if (isset($this->options['session'])) { $options['session'] = $this->options['session']; } if (isset($this->options['writeConcern'])) { $options['writeConcern'] = $this->options['writeConcern']; } return $options; } }