I need to query the Mongo database for items with a specific property starting with any prefix in the list. Now I have a code like this:
query = mymodel(terms__term__in=query_terms)
and this corresponds to objects that have an element in the "terms" list that has a "term" StringField that is explicitly found in the "query_terms" list. What I want to achieve are objects that have an item in the terms list that has the term StringField, starting with any prefix that appears in the query_terms list. Is it possible to do this in one query and without storing any possible prefix "term" in the predicate? EDIT: The solution below works fine, but now I have to find objects with conditions starting with each prefix in the 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 it does not work. As a result, I get the following error:
InvalidQueryError: Duplicate query conditions: terms__term__startswith
Any ideas?
source share