8

I want to check with mongodb if nickname and password are true im trying to use the $and operator with $regex but I can't get it work. Can someone help me a hand with it currently have this;

checkNickname: function(nickname, password){
    var reNick = new RegExp(nickname, "i"),
        rePass = new RegExp(password, "i");
    getProfile.find( { $and :  [{
        nickname: { $ne: { $regex: reNick } } },
        { password: { $ne: { $regex: rePass} } } 
    ]}, function(data){
        console.log(data)
    });
},
styvane
  • 49,879
  • 15
  • 119
  • 132
I NA
  • 137
  • 1
  • 6

2 Answers2

7

This will do it for you,

    checkNickname: function(nickname, password){
        var reNick = new RegExp("/^" + nickname + ".*/", "i"),
            rePass = new RegExp("/^" + password + ".*/", "i");
        getProfile.find({ 
                    nickname: { $not: reNick },  
                    password: { $not: rePass }
            }, function(err, data) {
            console.log(data)
        });
    }
Hakan Kose
  • 1,554
  • 7
  • 14
1

As mention you can't have $regex as argument to $ne but you can negate your regex.

checkNickname: function(nickname, password){
    var reNick = RegExp("^((?!"+nickname+").)*$", "i");
    var rePass = new RegExp("^((?!"+password+").)*$", "i");
    getProfile.find( { nickname: reNick, password: rePass }, function(err, data) {
        console.log(data)
    });
};
Community
  • 1
  • 1
styvane
  • 49,879
  • 15
  • 119
  • 132