1

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"]


Edit: Is there anyway to pair up an $overlap with $iLike somehow?

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!

1
  • wrong data structure. Refer the postgresql arrays documentation (it says if you are searching your arrays you got the wrong strcuture) Commented Dec 12, 2016 at 3:43

1 Answer 1

1

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
})
Sign up to request clarification or add additional context in comments.

1 Comment

I've tried this already. You can't compare an array with a string iirc. Thanks for the attempt though!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.