Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

Given that I have a model called Model with a column called items which holds an array of strings, how can I query to see whether a string queryString is in the array or has a similar element in the array?

Example:

items: ["cat", "dog", "bird", "turtle", "doe" ]

queryString = ["%do%","%b"]

Should return:

animalArray = ["dog", "doe", "bird"]


I've attempted this, except it doesn't seem to return anything of value. $in searches the elements in queryString but not necessary the elements in items, so I'm a little stumped on how I can change this up to see if it can check for all combination of elements in items and queryString. - or if it's possible at all!

Model.findAll({
    where: {
        items: { $in: { $iLike: { $any: queryString } } }
    }
}).then(function(array){
    // Do whatever here with array
})

$iLike is a special Postgres thing in Sequelize

Thanks!

share|improve this question

Try this solution.

Step 1: Create a new array which stored your like conditions

var conditions = [];

var queryString = ["%do%","%b"];

Step 2: loop your queryString values

for(var x in queryString) {
    // do something
}

Step 3: inside loop just append your $like condition

conditions.push({
    items: {
        $like: queryString[x]
    }
});

So your array would be like this

[{
    items: {
        $like: "%do%"
    }
},{
    items: {
        $like: "%b"
    }
}]

So your sequelize query would be like this

var conditions = [];
var queryString = ["%do%","%b"];

for(var x in queryString) {
    conditions.push({
        items: {
            $like: queryString[x]
        }
    });
}

Model.findAll({
    where: {or: conditions}
}).then(function(array){
    // Do whatever here with array
})
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.