yii\mongodb\QueryBuilder::buildSimpleCondition PHP Method

buildSimpleCondition() public method

Creates an Mongo condition like {$operator:{field:value}}.
public buildSimpleCondition ( string $operator, array $operands ) : string
$operator string the operator to use. Besides regular MongoDB operators, aliases like `>`, `<=`, and so on, can be used here.
$operands array the first operand is the column name. The second operand is a single value that column value should be compared with.
return string the generated Mongo condition.
    public function buildSimpleCondition($operator, $operands)
    {
        if (count($operands) !== 2) {
            throw new InvalidParamException("Operator '{$operator}' requires two operands.");
        }
        list($column, $value) = $operands;
        if (strncmp('$', $operator, 1) !== 0) {
            static $operatorMap = ['>' => '$gt', '<' => '$lt', '>=' => '$gte', '<=' => '$lte', '!=' => '$ne', '<>' => '$ne', '=' => '$eq', '==' => '$eq'];
            if (isset($operatorMap[$operator])) {
                $operator = $operatorMap[$operator];
            } else {
                throw new InvalidParamException("Unsupported operator '{$operator}'");
            }
        }
        return [$column => [$operator => $value]];
    }