admin管理员组文章数量:1332353
I want do something, like what I can do in Mongoose:
Customers.find({
$or: [
{firstName: {$in: RegExpArray}},
{lastName: {$in: RegExpArray}},
{email: {$in: RegExpArray}}
]
}).limit(50);
I tried:
Customers.findAll({
where: {
$or: [
{firstName: {$iLike: {$in: RegExpArray}}},
{lastName: {$iLike: {$in: RegExpArray}}},
{email: {$iLike: {$in: RegExpArray}}}
]
},
limit: 50});
Also I tried use $regex
instead of $iLike
, or pass array [ '%someString%', '%someString%', '%someString%'...]
instead of RegExpArray
what should work, but non of this worked. Is it any way to use regex in query?
I want do something, like what I can do in Mongoose:
Customers.find({
$or: [
{firstName: {$in: RegExpArray}},
{lastName: {$in: RegExpArray}},
{email: {$in: RegExpArray}}
]
}).limit(50);
I tried:
Customers.findAll({
where: {
$or: [
{firstName: {$iLike: {$in: RegExpArray}}},
{lastName: {$iLike: {$in: RegExpArray}}},
{email: {$iLike: {$in: RegExpArray}}}
]
},
limit: 50});
Also I tried use $regex
instead of $iLike
, or pass array [ '%someString%', '%someString%', '%someString%'...]
instead of RegExpArray
what should work, but non of this worked. Is it any way to use regex in query?
- Can you give me a example of how the sql query would look like? – user3254198 Commented Oct 3, 2016 at 23:07
- actually I can't. I'm new in SQL, always have been working with noSQL, where you can easily use regex in query like in first example. – Sarkis Arutiunian Commented Oct 3, 2016 at 23:15
-
inside your sequelize config where you have your database info add
logging: true
. That should print the sql statements that are being run. – user3254198 Commented Oct 3, 2016 at 23:25 - forgot to mention. post us the sql statements that are being run use gist.github. to avoid the clutter in ments. – user3254198 Commented Oct 3, 2016 at 23:52
-
1
Can you show me the regular expression? Hopefully we can translate that into a
LIKE
statement instead. Out of the box sql supports aLIKE
attribute which is similar to a regex. – user3254198 Commented Oct 4, 2016 at 0:25
1 Answer
Reset to default 5Try this instead:
let searchFor = [ '%someString%', '%someString%', '%someString%'...]
// basically turning the items into objects with "$iLike" as the key
searchFor = searchFor.map((item) => {
return {$iLike: item};
});
Customer.findAll({
where: {
$or: [
{firstName: {$or: searchFor}},
{lastName: {$or: searchFor}},
{email: {$or: searchFor}}
]
},
order: [['createdAt', 'DESC']],
limit: 50
})
本文标签: javascriptsequelizejsuse RegEx to queryStack Overflow
版权声明:本文标题:javascript - sequelizejs, use RegEx to query - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742322892a2453146.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论