mongodm
mongodm copied to clipboard
Regex query is not working.
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.
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")) );