javascript - sequelizejs, use RegEx to query - Stack Overflow

I want do something, like what I can do in Mongoose:Customers.find({$or: [{firstName: {$in: RegExpArray

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?

Share Improve this question edited Oct 4, 2016 at 0:25 Sarkis Arutiunian asked Oct 3, 2016 at 19:32 Sarkis ArutiunianSarkis Arutiunian 1,2913 gold badges17 silver badges36 bronze badges 8
  • 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 a LIKE attribute which is similar to a regex. – user3254198 Commented Oct 4, 2016 at 0:25
 |  Show 3 more ments

1 Answer 1

Reset to default 5

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742322893a4422170.html

相关推荐

  • javascript - sequelizejs, use RegEx to query - Stack Overflow

    I want do something, like what I can do in Mongoose:Customers.find({$or: [{firstName: {$in: RegExpArray

    12小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信