1

How can I query a queryset returned by MongoEngine? Based on the documentation I couldn't find any information about querying querysets: http://docs.mongoengine.org/apireference.html#mongoengine.queryset.QuerySet

import datetime
thirty_days_in_the_past = datetime.datetime.now() - datetime.timedelta(days=30)

def last_messages(from_date):
    messages = Messages.objects(sent_at__gt=from_date)
    return messages

messages = last_messages(thirty_days_in_the_past)

Then I would like to get messages with a particular subquery such as finding an author_id "ABC":

messages.query(author_id="ABC")

The reason I am looking to do this as the initial query itself is used by a component and a subquery of it is used by another component and I would like to reuse the query.

1 Answer 1

1

Just call the queryset with the narrowed-down query or use .filter() (which is just an alias). The mongoengine documentation does not really mention the possibility to just call the queryset with another query (and the existence of filter) for some reason.

author_messages = messages(author_id="ABC")
# or
author_messages = messages.filter(author_id="ABC")

"Fun fact", the django orm, which mongoengine took heavy inspiration from (the whole query language, including the __ keyword syntax), does mention the filter method in its docs :)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.