在mongodb中查找不区分大小写的php中的用户名 [英] case insensitive find in mongodb for usernames in php

查看:174
本文介绍了在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.

这篇关于在mongodb中查找不区分大小写的php中的用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆