Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7850283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:48:01+00:00 2026-06-02T18:48:01+00:00

I need to query Mongo database for elements that have a certain property beginning

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T18:48:03+00:00Added an answer on June 2, 2026 at 6:48 pm

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have field Tdate (text type) on my Table MyTbl i need query that
I need a query which can do the following operation. I have a table
I need a query that will return a table where each column is the
I need to query a Sybase database which has a lot of data in
I have three table I need to query table 1,2 and 3 and select
I don't have any prior experience with database, but I have to use it
So, I need to write something in Rails 3 that does a query to
I have a Mango WP7.5 app that uses a local SqlCe database. I would
If i have a mongo document id as a string how do I query
I've got 2 tables that I need query and return a result set of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.