node.jssails.jswaterline

find() with equal and like in sails


I'd like to execute a query like this,

select * from user where user.status = 'ban' and
user.status = 'new' and
user.username like 'fo%' // in code I will get this from req.param('user')

I know how to do like this,

var username = req.param('user');
var status = req.param('status');
User.find().where({ status : [status, 'new'], username : username }).exec(function(err, users){
        console.log(users);
});

But I don't know how to use "like" for the username.

How should I do. Thanks.


Solution

  • var username = req.param('user');
    var status = req.param('status');
    User.find({
        status: [status, 'new'],
        username: {
            'startsWith': username
        }
    }).exec(function(err, users) {
        console.log(users);
    });
    

    Give that a try

    and for reference, here is a link to documentation about the waterline query format https://github.com/balderdashy/waterline-docs/blob/master/query-language.md