Home > other >  Combining regex with $in operator in mongodb
Combining regex with $in operator in mongodb

Time:04-10

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

CodePudding user response:

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.
  • Related