1

I'm building regex queries for mongo

        s_list = [f"/^.*{x}.*/i" for x in s_list]
        print(s_list)
        query: dict = resumes_collection.find({
            "first_name": {
                "$in": s_list
            },
        })

This regex is supposed to get all data regardless of case sensitivity etc if we have loop in the database if we enter ^.*OP.*/i or ^.*oO.*/i will return loop in either case Currently this returns nothing. https://www.mongodb.com/docs/manual/reference/operator/query/regex/ Docs for reference. Using motor to interact with mongodb, motor runs pymongo under the hood

1
  • 1
    You do not need [f"/^.*{x}.*/i" for x in s_list]. Remove the first line and try "first_name": { "$regex": re.compile("|".join(s_list), re.I) }. Commented Apr 9, 2022 at 9:20

1 Answer 1

1

You can use

query: dict = resumes_collection.find({
            "first_name": {
                "$regex": re.compile("|".join(s_list), re.I)
            },
        })

Notes:

  • The [f"/^.*{x}.*/i" for x in s_list] generates a list of strings that are meant to be parsed as regex, but the $in operator will treat them as strings
  • /.../ regex literal notation is not supported in Python, you need to define a regex object using re.compile
  • "|".join(s_list) generates a single pattern out of all the s_list strings joining them with a | OR operator. You do not need to match the whole string, adding .* on both sides is not necessary and does more harm in fact.
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.