Is there a way to print the field value when error occurs in aggregation?

Error code and error message doesn’t tell much

{“t”:{"$date":“2022-04-06T13:42:43.791-04:00”},“s”:“W”, “c”:“COMMAND”, “id”:23799, “ctx”:“conn69691”,“msg”:“Aggregate command executor error”,“attr”:{“error”:{“code”:51156,“codeName”:“Location51156”,“errmsg”:“Error occurred while executing the regular expression in $regexMatch. Result code: -21”},“stats”:{},“cmd”:{“aggregate”:“logRE”,“pipeline”:[{"$match":{"$expr":{"$regexMatch":{}}}}]}},“truncated”:{“cmd”:{“pipeline”:{“0”:{"$match":{"$expr":{"$regexMatch":{“input”:{“type”:“string”,“size”:551965}}}}}}}},“size”:{“cmd”:552225}}

Hello @Xiaoyan_Xu and Welcome to the community!!

It looks like -21 error code is returned by PCRE which is an external library largely used for regex.

We can further help if you describe what are you trying to achieve

Thank you @Imad_Bouteraa ,

Say I have a collection, in which the documents look like this:

  pattern: ABC
  pattern: QWE

The field “pattern” is just a pattern that can be used in regex.

Now I would like to find all documents of which the pattern can be matched to a text.
Say the text looks like this: ABCEFD
I want to get all documents of which the pattern got matched to the text. So in this case the document { pattern: ABC } is returned.
Now I have a bunch of this pattern documents and I have a very long text. I executed the following code:

                "$expr": {
                    "$regexMatch": {
                    "input": text,
                    "regex": "$pattern"
        { "$out" : coll_matched}

And got this error.

Is it caused by invalid pattern? I would like to find out the cause, maybe to find which specific document causes this error.

Or is the aggregation strategy and its query are not appropriate for my use case? Is there any other way to achieve this “matching” task?

If your field is named pattern in your coll_patterns collection then that is your input and text is your regex.