在mongodb中查找不区分大小写的php中的用户名

问题描述:

我正在尝试找到一种使用PHP以不区分大小写的方式查询mongodb的方法,因为当前在检查用户"Username"和"username"时可以同时使用不同的用户名...

I am trying to find a way to query mongodb in a case insensitive way using PHP because currently when checking a user "Username" and "username" can both be diffrent usernames...

$cursor = $collection->findOne(array('username' => /{$_POST['value']}/i));
$lowerInput = strtolower($_POST['value']);
$username = strtolower($cursor['username']);
if($lowerInput == $username){
    echo "That username appears to be in our database!";
}

我尝试过此操作,但是游标只查找区分大小写的匹配项,因此,如果游标值有一个小写的匹配项,则只会小写.

I tried this but the cursor only looks for a case sensitive match so it will only lowercase the cursor value if it has one.

PHP Mongo驱动程序具有内部Regex对象:

PHP Mongo driver has an internal Regex Object:

$cursor = $collection->findOne(
  array('username' => new MongoRegex("/$_POST['value']/i")
);

顺便说一句,我强烈建议检查$ _POST值,并可能将您的正则表达式转换为仅获取用户名(之前/之后没有更多=> new MongoRegex('/^' . $securevalue . '$/i')

And btw I strongly recommand to check $_POST value and probably transform you regex to get only username (without more before/after => new MongoRegex('/^' . $securevalue . '$/i')

我的回答不精确:如果可能,启动锚点可允许mongo在此查询上使用索引.

my answer wasn't precise: starting anchor allow mongo to use index on this query, if available.