Using 'andFilterWhere' with 'like' for integer fields in MongoDB

I try to make a filter for integer field in mongoDB using andFilterWhere.




$query->andFilterWhere(['like', 'code', $this->code ]);

This method work fine for String value but for integer value doesn’t work. How to use this method for integer fields?

You should use like a below code to search integer values.


$query->andFilterWhere(['code' => $this->code]);

I’d personally use andFilterCompare. It’s the same as the ‘like’ but you can also use >,<,=,!= and combinations of them ie >=1 or >1 <3 for searching.


$query->andFilterCompare('code', $this->code)

This example helps me to solve my issue:


 if ($this->code)

            $query->andFilterWhere([

                '$where' => '/' . $this->code . '/.test(this.code')'

            ]);