Reputation: 755
How to find on the basis of case-insensitive value in MongoDB PHP?
I am using below lines of code
$query = ['VehicleNumber' => $arg];
$cursor = $this->collection->find($query);
I want to find the data on the basis of VehicleNumber but it should be able find the data on the basis of case insensitive arg. For e.g if there are data in the table with vehicle numbers like JK AB123, JK DE245 etc. Now if arg contains jk Ab123, it should be able to find that data.
Please help !!!
Upvotes: 2
Views: 829
Reputation: 17673
You should use \MongoDB\BSON\Regex, with the "i" flag (Case insensitivity to match upper and lower cases).
You should also escape the string to not match any regex special characters; for this you can use preg_quote.
$query = ['VehicleNumber' => new \MongoDB\BSON\Regex( preg_quote($arg),"i")];
Upvotes: 2
Reputation: 787
You can use $regex for find case sensitive values.
db.col.find({"VehicleNumber": { "$regex": /^jk Ab123/i } })
Upvotes: 0