Cml\Db\MongoDB\MongoDB::update PHP Method

update() public method

根据key更新一条数据
public update ( string $key, array | null $data = null, boolean $and = true, mixed $tablePrefix = null ) : boolean
$key string eg 'user-uid-$uid' 如果条件是通用whereXX()、表名是通过table()设定。这边可以直接传$data的数组
$data array | null
$and boolean 多个条件之间是否为and true为and false为or
$tablePrefix mixed 表前缀 不传则获取配置中配置的前缀
return boolean
    public function update($key, $data = null, $and = true, $tablePrefix = null)
    {
        is_null($tablePrefix) && ($tablePrefix = $this->tablePrefix);
        $tableName = $condition = '';
        if (is_array($data)) {
            list($tableName, $condition) = $this->parseKey($key, $and, true, true);
        } else {
            $data = $key;
        }
        $tableName = empty($tableName) ? $this->getRealTableName(key($this->table)) : $tablePrefix . $tableName;
        if (empty($tableName)) {
            throw new \InvalidArgumentException(Lang::get('_PARSE_SQL_ERROR_NO_TABLE_', 'update'));
        }
        $condition += $this->sql['where'];
        if (empty($condition)) {
            throw new \InvalidArgumentException(Lang::get('_PARSE_SQL_ERROR_NO_CONDITION_', 'update'));
        }
        $bulk = new BulkWrite();
        $bulk->update($condition, ['$set' => $data], ['multi' => true]);
        $result = $this->runMongoBulkWrite($tableName, $bulk);
        Cml::$debug && $this->debugLogSql('BulkWrite UPDATE', $tableName, $condition, $data);
        return $result->getModifiedCount();
    }