Search code examples
pythonmongodbmongoenginenosql

MongoEngine query list for objects having properties starting with prefixes specified in a list


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: The 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?


Solution

  • If your querying a term for it's value, you can filter the values that begin with a perfix like so:

    MyModel.objects.filter(terms__term__startswith='foo')
    

    If you need to filter for multiple prefixes you'll have to create Q objects for that:

    MyModel.objects.filter(Q(terms__term__startswith='foo') | Q(terms__term__startswith='bar'))
    

    If you need to create the query dynamically:

    prefixes = ['foo', 'bar', 'baz']
    query = reduce(lambda q1, q2: q1.__or__(q2), 
                   map(lambda prefix: Q(terms__term__startswith=prefix), prefixes))
    MyModel.objects.filter(query)