简体   繁体   中英

is there a way to use regex over string array within $filter (aggregation)

currently using this .find({"strings": {"$elemMatch": {"$in": [/test/si]}}}) to find a string in the "strings" array. Now i also need to search this string HERE

.aggregate([
{
    $match: {"_id" : ObjectId("5c5c59c689c4cf027b382999")  }
},
{ $project: {      
    data: {
        $filter: {
            input: '$data',
            as: 'item',
            cond: {

--------------------------------- HERE

                }
            }
        }
    }}
])

I already tried a lot of similar things like eg:

.aggregate([ {
             $match:
            {"_id" : ObjectId("5c5c59c689c4cf027b382999")  }
          },
            { $project: {      
                data: {
                    $filter: {
                        input: '$data',
                        as: 'item',
                        cond: { $in: [/test/si, "$$item.strings"] }
                    }
               }
            }}
        ])

I currently can not think about a way how to do this. Is there any way to do this in Version 3.6?

if you can help in any way: thanks

greetings HM

I've made a workaround:

"$filter": {
                    "input": "$data",
                    "as":    "item",
                    "cond": {
                        "$lt": {-1, {"$indexOfBytes": ["$$item.searchString", "text"]}}}
           }

But this works really badly without regex and I can not match a string[]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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