0

I'm using django and I need to get some info from my MongoDB using a regular expression.

Running this directly into the mongo shell I get the results that I want:

db.recipe.find({"title.spanish": {"$regex" : /s[aáAÁ]lm[oóOÓ]n/i}})

Im trying to do the same query in django using raw but it is giving me a syntax error that I can't figure out.

recipes = Recipe.objects(__raw__={'title.spanish' : {'$regex' : /s[aáAÁ]lm[oóOÓ]n/i}})

Any suggestions of how can I do this query in django?

alex
  • 18
  • 3

1 Answers1

2

You should try the following:

regx = re.compile('s[aáAÁ]lm[oóOÓ]n', re.IGNORECASE)

recipes = Recipe.objects(__raw__={'title.spanish': {'$regex': regx.pattern}})

And verify if objects queryset manager is not adding a filter previously. I usually add all method to model classes (this method returns all the existent documents in the mongo collection) so you could try with that instead objects and verify your results.

Please, let me know if this works for you.

imarban
  • 1,017
  • 1
  • 9
  • 24