Search code examples
phpregexmongodbcompass

Command works in mongodb compass but not in php


_id:1
email:"[email protected]"

This is my collection. When I write

{ email: /^J/i  }

in compass it works absolutely fine, but in php

$user_collection->find(
    ["email" => ['$regex' => "/^j/i"]]
);

finds nothing


Solution

  • In your example /^J/i, the i is an option flag to request case-insensitive search.

    When using $regex you need to use a separate options field:

    ['$regex' => "^j", '$options'=>"i"]