我正在尝试找到一种使用 PHP 以不区分大小写的方式查询 mongodb 的方法,因为目前在检查用户时“用户名”和“用户名”都可以是不同的用户名...
$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!";
}
我试过了,但游标只查找区分大小写的匹配项,因此它只会将游标值小写(如果有的话)。
最佳答案
PHP Mongo 驱动程序有一个内部正则表达式对象:
$cursor = $collection->findOne(
array('username' => new MongoRegex("/$_POST['value']/i")
);
顺便说一句,我强烈建议检查 $_POST 值,并可能将您的正则表达式转换为仅获取用户名(没有更多之前/之后 => new MongoRegex('/^' . $securevalue . '$/i')
编辑:我的回答不准确:起始 anchor 允许 mongo 在此查询上使用索引(如果可用)。
关于php - 在 mongodb 中查找不区分大小写的 php 用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9100969/