Cml\Db\MongoDB\MongoDB::decrement PHP Method

decrement() public method

指定字段的值-1
public decrement ( string $key, integer $val = 1, string $field = null, mixed $tablePrefix = null ) : boolean
$key string 操作的key user-id-1
$val integer
$field string 要改变的字段
$tablePrefix mixed 表前缀 不传则获取配置中配置的前缀
return boolean
    public function decrement($key, $val = 1, $field = null, $tablePrefix = null)
    {
        list($tableName, $condition) = $this->parseKey($key, true);
        if (is_null($field) || empty($tableName) || empty($condition)) {
            return false;
        }
        $val = abs(intval($val));
        is_null($tablePrefix) && ($tablePrefix = $this->tablePrefix);
        $tableName = $tablePrefix . $tableName;
        $bulk = new BulkWrite();
        $bulk->update($condition, ['$inc' => [$field => -$val]], ['multi' => true]);
        $result = $this->runMongoBulkWrite($tableName, $bulk);
        Cml::$debug && $this->debugLogSql('BulkWrite DEC', $tableName, $condition, ['$inc' => [$field => -$val]]);
        return $result->getModifiedCount();
    }