简体   繁体   English

序列化PostgreSQL:查询以查看字符串是否在数组中

[英]Sequelize PostgreSQL: query to see if string is within an array

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? 假设我有一个名为Model ,该Model的一列称为items ,该列包含一个字符串数组,那么如何查询以查看字符串queryString是在数组中还是在数组中具有相似的元素?

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? 编辑:是否有某种方式可以将$ iLike与$ overlap配对?

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

$iLike is a special Postgres thing in Sequelize $iLikeSequelize中特殊的Postgres 工具

Thanks! 谢谢!

Try this solution. 试试这个解决方案。

Step 1: Create a new array which stored your like conditions 步骤1:创建一个新数组,存储您的条件

var conditions = [];

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

Step 2: loop your queryString values 步骤2:循环查询字符串值

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

Step 3: inside loop just append your $like condition 步骤3:内部循环只需附加您的$like条件

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
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM