purekid / mongodm

MongoDB ORM that includes support for references,embed and multilevel inheritance.
MIT License
200 stars 47 forks source link

Regex query is not working. #96

Open prashanthsun9 opened 7 years ago

prashanthsun9 commented 7 years ago

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 commented 7 years ago

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")) );