1 year ago

#383886

test-img

Sean

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

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

    coll_patterns.aggregate([
        {
            "$match": 
            {
                "$expr": {
                    "$regexMatch": {
                    "input": text,
                    "regex": "$pattern"
                    }
                }
            }
        },
        { "$out" : coll_matched}
    ])

And got this error

pymongo.errors.OperationFailure: 
PlanExecutor error during aggregation :: caused by :: Error occurred while executing the regular expression in $regexMatch. 
Result code: -21, full error: 
{'ok': 0.0, 'errmsg': 'PlanExecutor error during aggregation :: caused by :: Error occurred while executing the regular expression in $regexMatch. 
Result code: -21', 'code': 51156, 'codeName': 'Location51156'}

The error code and error message don't tell much. I would like to find out the cause, maybe to find which specific document causes this error.

How can I do this? Or is the aggregation strategy and its query are not good for my use case? Is there any other way to achieve this "matching" task?

python-3.x

regex

mongodb

pymongo

0 Answers

Your Answer

Accepted video resources