Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Found unknown operator in query: >

Tags:

mongodb

yii2

I am using Yii 2 and Mongo DB. Below condition throws error.

 $query->andFilterWhere(['>', 'discount', 50]);

frontend\controllers\DealController.php

 $searchModel = new DealSearch();

 $dataProvider = $searchModel->searchDeals(Yii::$app->request->queryParams);

frontend\models\search\DealSearch.php

public function searchDeals($params)
    {        
        $query = deal::find();        
        $dataProvider = new ActiveDataProvider([
            'query' => $query,          
            'pagination' => [
                'pageSize' => 3 ,                              
            ], 
        ]);

        $this->load($params);        
        if (!$this->validate()) {            
            return $dataProvider;
        }

        $query->andFilterWhere([
            'status' => '1',
            'approved_status' => '1',           
           //'discount >=' => $this->discount[1],
            'category_id' => $this->category_id,                       
           'state_id' => $this->state_id,
           'city_id' => $this->city_id,           
        ]);

         $query->andFilterWhere(['like', 'deal_title', $this->deal_title])
                ->andFilterWhere(['>', 'discount', 50]);

        return $dataProvider;
    }
like image 232
Mohd Bashir Avatar asked Mar 12 '26 16:03

Mohd Bashir


1 Answers

Seems, that operators like: >, <, >=, =< are not supported by mongodb yii extension. But you can use between operator directly:

$query->andFilterWhere(['between', 'discount', 50, 400]);

where 400 - is some big value(related to your purposes). And the result query will be looks like this:

{"discount":{"$gte":50,"$lte":400}}
like image 162
joni jones Avatar answered Mar 14 '26 09:03

joni jones