/** * {@inheritdoc} */ protected function performRollback($table, array $records) { $bulk = new BulkWrite(); foreach ($records as $record) { $id = $record['_id']; unset($record['_id']); $bulk->delete(array('_id' => $id)); } $this->conn->executeBulkWrite($this->getNamespace($table), $bulk); }
/** * Removes documents from collection * * @param array $query * * @return Result */ public function remove($query) { $result = new Result(); $bulk = new BulkWrite(); $bulk->delete($query, ['limit' => 0]); $dbResult = $this->executeBulkWrite($bulk); if ($dbResult->getDeletedCount() === 0) { $result->setError(Result::ERROR_CANNOT_DELETE_RECORD, implode('. ', $dbResult->getWriteErrors())); } return $result; }
/** * Write into database * * @param $collection * @param $command * @param $data * @return mixed */ public function write($collection, $command, $data) { // Make sure the database is connected $this->_connection or $this->connect(); // Set the last query $this->_last_query = $data; // Configurations $config = $this->_config; // Exec bulk command $bulk = new BulkWrite(); switch ($command) { case 'insert': $data['_id'] = new \MongoDB\BSON\ObjectID(); $bulk->insert($data); break; case 'update': $bulk->update($data[0], $data[1], $data[2]); break; case 'delete': $bulk->delete($data[0], $data[1]); break; } try { $writeConcern = new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY, 1000); $response = $this->_connection->executeBulkWrite($config['database'] . '.' . $collection, $bulk, $writeConcern); } catch (\MongoDB\Driver\Exception\BulkWriteException $e) { //print_r($e);die(); echo $e->getMessage(), "\n"; //exit; } return $response; }
/** * Execute the operation. * * @see Executable::execute() * @param Server $server * @return DeleteResult */ public function execute(Server $server) { $bulk = new Bulk(); $bulk->delete($this->filter, ['limit' => $this->limit]); $writeConcern = isset($this->options['writeConcern']) ? $this->options['writeConcern'] : null; $writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $writeConcern); return new DeleteResult($writeResult); }
/** * Execute commands batch (bulk). * @param string $collectionName collection name. * @param array $options batch options. * @return array array of 2 elements: * * - 'insertedIds' - contains inserted IDs. * - 'result' - [[\MongoDB\Driver\WriteResult]] instance. * * @throws Exception on failure. * @throws InvalidConfigException on invalid [[document]] format. */ public function executeBatch($collectionName, $options = []) { $databaseName = $this->databaseName === null ? $this->db->defaultDatabaseName : $this->databaseName; $token = $this->log([$databaseName, $collectionName, 'bulkWrite'], $this->document, __METHOD__); try { $this->beginProfile($token, __METHOD__); $batch = new BulkWrite($options); $insertedIds = []; foreach ($this->document as $key => $operation) { switch ($operation['type']) { case 'insert': $insertedIds[$key] = $batch->insert($operation['document']); break; case 'update': $batch->update($operation['condition'], $operation['document'], $operation['options']); break; case 'delete': $batch->delete($operation['condition'], isset($operation['options']) ? $operation['options'] : []); break; default: throw new InvalidConfigException("Unsupported batch operation type '{$operation['type']}'"); } } $this->db->open(); $server = $this->db->manager->selectServer($this->getReadPreference()); $writeResult = $server->executeBulkWrite($databaseName . '.' . $collectionName, $batch, $this->getWriteConcern()); $this->endProfile($token, __METHOD__); } catch (RuntimeException $e) { $this->endProfile($token, __METHOD__); throw new Exception($e->getMessage(), $e->getCode(), $e); } return ['insertedIds' => $insertedIds, 'result' => $writeResult]; }
/** * Execute the operation. * * @see Executable::execute() * @param Server $server * @return BulkWriteResult */ public function execute(Server $server) { $bulk = new Bulk(['ordered' => $this->options['ordered']]); $insertedIds = []; foreach ($this->operations as $i => $operation) { $type = key($operation); $args = current($operation); switch ($type) { case self::DELETE_MANY: case self::DELETE_ONE: $bulk->delete($args[0], $args[1]); break; case self::INSERT_ONE: $insertedId = $bulk->insert($args[0]); if ($insertedId !== null) { $insertedIds[$i] = $insertedId; } else { // TODO: This may be removed if PHPC-382 is implemented $insertedIds[$i] = is_array($args[0]) ? $args[0]['_id'] : $args[0]->_id; } break; case self::REPLACE_ONE: case self::UPDATE_MANY: case self::UPDATE_ONE: $bulk->update($args[0], $args[1], $args[2]); } } $writeConcern = isset($this->options['writeConcern']) ? $this->options['writeConcern'] : null; $writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $writeConcern); return new BulkWriteResult($writeResult, $insertedIds); }
/** * 根据key值删除数据 * * @param string $key eg: 'user-uid-$uid' * @param bool $and 多个条件之间是否为and true为and false为or * * @return boolean */ public function delete($key = '', $and = true) { $tableName = $condition = ''; empty($key) || (list($tableName, $condition) = $this->parseKey($key, $and, true, true)); $tableName = empty($tableName) ? $this->getRealTableName(key($this->table)) : $this->tablePrefix . $tableName; empty($tableName) && \Foundation\throwException(Lang::get('_PARSE_SQL_ERROR_NO_TABLE_', 'delete')); $condition += $this->sql['where']; empty($condition) && \Foundation\throwException(Lang::get('_PARSE_SQL_ERROR_NO_CONDITION_', 'delete')); $bulk = new BulkWrite(); $bulk->delete($condition); $result = $this->runMongoBulkWrite($tableName, $bulk); $GLOBALS['debug'] && $this->debugLogSql('BulkWrite DELETE', $this->tablePrefix . $tableName, $condition); return $result->getDeletedCount(); }
/** * Execute the operation. * * @see Executable::execute() * @param Server $server * @return BulkWriteResult */ public function execute(Server $server) { $options = ['ordered' => $this->options['ordered']]; if (isset($this->options['bypassDocumentValidation']) && \MongoDB\server_supports_feature($server, self::$wireVersionForDocumentLevelValidation)) { $options['bypassDocumentValidation'] = $this->options['bypassDocumentValidation']; } $bulk = new Bulk($options); $insertedIds = []; foreach ($this->operations as $i => $operation) { $type = key($operation); $args = current($operation); switch ($type) { case self::DELETE_MANY: case self::DELETE_ONE: $bulk->delete($args[0], $args[1]); break; case self::INSERT_ONE: $insertedId = $bulk->insert($args[0]); if ($insertedId !== null) { $insertedIds[$i] = $insertedId; } else { $insertedIds[$i] = \MongoDB\extract_id_from_inserted_document($args[0]); } break; case self::REPLACE_ONE: case self::UPDATE_MANY: case self::UPDATE_ONE: $bulk->update($args[0], $args[1], $args[2]); } } $writeConcern = isset($this->options['writeConcern']) ? $this->options['writeConcern'] : null; $writeResult = $server->executeBulkWrite($this->databaseName . '.' . $this->collectionName, $bulk, $writeConcern); return new BulkWriteResult($writeResult, $insertedIds); }
public function remove($namespace, $query = [], $options = []) { $bulk = new BulkWrite(); $bulk->delete($query, $options); $this->result = $this->write($namespace, $bulk); return !$this->result->getWriteErrors(); }
/** * 根据key值删除数据 * * @param string $key eg: 'user-uid-$uid' * @param bool $and 多个条件之间是否为and true为and false为or * @param mixed $tablePrefix 表前缀 不传则获取配置中配置的前缀 * * @return boolean */ public function delete($key = '', $and = true, $tablePrefix = null) { is_null($tablePrefix) && ($tablePrefix = $this->tablePrefix); $tableName = $condition = ''; empty($key) || (list($tableName, $condition) = $this->parseKey($key, $and, true, true)); $tableName = empty($tableName) ? $this->getRealTableName(key($this->table)) : $tablePrefix . $tableName; if (empty($tableName)) { throw new \InvalidArgumentException(Lang::get('_PARSE_SQL_ERROR_NO_TABLE_', 'delete')); } $condition += $this->sql['where']; if (empty($condition)) { throw new \InvalidArgumentException(Lang::get('_PARSE_SQL_ERROR_NO_CONDITION_', 'delete')); } $bulk = new BulkWrite(); $bulk->delete($condition); $result = $this->runMongoBulkWrite($tableName, $bulk); Cml::$debug && $this->debugLogSql('BulkWrite DELETE', $tableName, $condition); return $result->getDeletedCount(); }
/** * {@inheritdoc} */ public function drop($table) { $bulk = new BulkWrite(); $bulk->delete(array()); $this->conn->executeBulkWrite($this->getNamespace($table), $bulk); }
/** * Deletes some collections instance. Returning true on success or false otherwise. * * @param array $filter * @param array $options * @param boolean $mode * @return boolean|int */ public static function deleteMany($filter = [], $options = [], $mode = true) { if (!is_array($filter)) { throw new Exception('The "filter" must be an array !'); } if (!is_array($options)) { throw new Exception('The "options" must be an array !'); } if ($mode) { $documents = static::find($filter, $options); if ($documents) { foreach ($documents as $document) { $document->delete(); } } return count($documents); } else { $className = get_called_class(); $collection = new $className(); $db = $collection->getDB(); $source = $collection->getSource(); if (empty($source)) { throw new Exception('Method getSource() returns empty string'); } /* Specify some command options for the update: * * * limit (integer): Deletes all matching documents when 0 (false). Otherwise, * only the first matching document is deleted. */ if (empty($options)) { $options = ["limit" => 0]; } // Create a bulk write object and add our delete operation $bulk = new BulkWrite(); $bulk->delete($filter, $options); /* Specify the full namespace as the first argument, followed by the bulk * write object and an optional write concern. MongoDB\Driver\WriteResult is * returned on success; otherwise, an exception is thrown. */ $result = $collection->getCollectionManager()->executeBulkWrite($collection, $db, $source, $bulk); if (empty($result->getWriteErrors())) { return $result->getDeletedCount(); } else { return false; } } }