I need to query Mongo database for elements that have a certain property beginning with any prefix in the list. Now I have a piece of code like this:
query = mymodel(terms__term__in=query_terms)
and this matches objects that have an item on a list "terms" that has StringField "term" explicitly occurring on a list "query_terms". What I want to achieve is having objects that have an item on a list "terms" that has StringField "term" beginning with any prefix that occurs on a list "query_terms". Is it possible to do it in one query and without storing every possible prefix of "term" in database? EDIT: Solution below works great but now I have to find objects with terms starting with every prefix on a list. I changed
query = reduce(lambda q1, q2: q1.__or__(q2),
map(lambda prefix: Q(terms__term__startswith=prefix)))
to
query = reduce(lambda q1, q2: q1.__and__(q2),
map(lambda prefix: Q(terms__term__startswith=prefix)))
but this does not work. I end up getting the following error:
InvalidQueryError: Duplicate query conditions: terms__term__startswith
Any ideas?