mongodm icon indicating copy to clipboard operation
mongodm copied to clipboard

Regex query is not working.

Open prashanthsun9 opened this issue 8 years ago • 1 comments

I am not sure what I am doing wrong but this below query is not working, can you help?

        if($this->input->has('filter')){
            $filter =  array( 'name' => array( '$regex' => $this->input->get('filter')) );
        }else{
            $filter = [];
        }
        $rawBrands = Brands::find($filter, ['name' => 1], [], 20, $skip);

I get no results, but if I executed the same query directly in mongodb I get lots of matched records, is $regex working in this lib? The result that i get is always empty, but it should not be the case, either I should get all the brands or the brands that match the filter and yes I do have lots of documents in brand collection which match the search criteria.

prashanthsun9 avatar Jul 12 '17 08:07 prashanthsun9

Found a solution my self. you could include this in your Documentation, we need to us MongoRegex.

$filter = array( 'name' => array( '$regex' => new MongoRegex("/^".$this->input->get('filter')."/i")) );

prashanthsun9 avatar Jul 13 '17 05:07 prashanthsun9