簡體   English   中英

有沒有辦法在 $filter(聚合)中的字符串數組上使用正則表達式

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

當前使用此.find({"strings": {"$elemMatch": {"$in": [/test/si]}}})在“strings”數組中查找字符串。 現在我還需要在這里搜索這個字符串

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

---------------------------------這里

                }
            }
        }
    }}
])

我已經嘗試了很多類似的事情,例如:

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

我目前想不出如何做到這一點。 有沒有辦法在 3.6 版中做到這一點?

如果您能以任何方式提供幫助:謝謝

問候 HM

我已經做了一個解決方法:

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

但是如果沒有正則表達式,這真的很糟糕,我無法匹配string[]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM