Rusty
Rusty

Reputation: 384

Returning mongo query regardless of case in node

I am using mongojs and am just trying to return a query regardless of upper or lower case. For example I have this

db.users.findOne({username: username, password: password}, function(err, docs) {
        if(docs) {
            currentUser.username = docs.username;
            res.render('home', {
                currentuser: currentUser
            });
        } else {
            res.render('index', {
                message: 'Invalid Credentials'
            });
        }
    });

username and password belong to a user object and are getting populated from a login page. For example is the username was HankHill - I would want the user returned whether it was hankhill or HankHill in the database.

I am just familierizing myself with both of these technologies so I am not sure the best way to approach this.

Upvotes: 0

Views: 35

Answers (1)

ManishKumar
ManishKumar

Reputation: 1554

You can make query something like this. it will ignore cases

db.users.findOne({username: { $regex: new RegExp("^" + username + "$", "i")}, password: password}, function(err, docs) {
        if(docs) {
            currentUser.username = docs.username;
            res.render('home', {
                currentuser: currentUser
            });
        } else {
            res.render('index', {
                message: 'Invalid Credentials'
            });
        }
    });

Upvotes: 1

Related Questions