Search code examples
pythonregexmongodb-queryrestful-architectureeve

How to create a Case-Insensitive regex in eve rest framework


I am using EVE REST Framework and POSTMAN

In my web API the search for a particular value takes place only when the exact match for the field value is given. It is case sensitive I would like make case insensitive search so that i could retrieve all the values matching the search criteria even if the search term is not given fully.

'url': 'regex("[\w]+")',

If I type just a or A I want all the values that start with a and A to be fetched.

Thanks in advance...


Solution

  • A query example would be:

    /?where={"field":{"$regex":"^(?i)value.*"} }
    

    This would find all documents where field has values starting with "value", case insensitive ("Value", "value", "VALUE", etc.)

    Remember though, by default $regex is blacklisted in Eve (MONGO_QUERY_BLACKLIST = ['$where', '$regex']). So if you really want it enabled, you also have to add this line to you settings.py:

    MONGO_QUERY_BLACKLIST = ['$where']