0

I have a list of distinct strings.

lst = ['.\*123.\*','.\*252.\*','.\*812.\*','.\*135.\*']

I want to perform an aggregation operation such that my $match looks like the following:

 {"$match":{"Var":{"$in":lst}}}  

The var field in MongoDb records is a string containing numbers:
e.g. "abc123", "haaofalfa812", etc. I want to match this Var to a regular expression. If the variables in the lst were less, i could've done this...

{"$match":{"Var":{"$in":[re.compile('.*123.*'),re.compile('.*252.*'),re.compile('.*812.*')]}}}

But since it is a lst already initialized and contains a lot of elements, what should I do?

I tried the following but this doesn't work too...

{"$match":{"Var":{"$in":[re.compile(x for x in lst)]}}}  

I get the following error for obvious reasons.

TypeError: first argument must be string or compiled pattern

1 Answer 1

3

Your list comprehension is wrong. I reckon what you wanted to do is this:

[re.compile(x) for x in lst]

That TypeError comes from trying to pass generator(x for x in lst statement) to re.compile()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.