Reputation: 195
So this is how my models look like. Class A is having an EmbeddedDocumentListField of SlotTime.
class SlotTime(EmbeddedDocument):
# this is having minutes like 780 for 1pm.
start_time = IntField(required=True)
end_time = IntField(required=True)
class A(Document):
name = StringField(primary_key=True)
slot_hours = EmbeddedDocumentListField(SlotTime)
SlotTime has a list of objects with start and end time value.
[<SlotTime: SlotTime object>,<SlotTime: SlotTime object>]
and now I want to make a query that will return me the results that have start_time greater that to a given value.
want a somthing simliar to this query:A.objects.get(name__exact='xyz').slot_hours.filter(start_time__gte=780)
tried this but this is returning all the value. A.objects.filter(name__exact='xyz',slot_hours__start_time__gte=780)[0].slot_hours
Can someone please help me in how I can do this? Thank you!
Upvotes: 4
Views: 1786
Reputation: 6354
Assuming you have the schema that you provided, you could achieve that simply with the __gte
operator. See below:
A.objects(name__exact='xyz', slot_hours__start_time__gte=780)
A.objects(name__exact='xyz').filter(slot_hours__start_time__gte=780)
Upvotes: 1
Reputation: 47866
I don't think MongoEngine currently supports further filtering out the EmbeddedDocuments
received using the .filter()
query you used above. That's why accessing slot_hours
returns all the SlotTime
objects for that object instead of only those SlotTime
objects having start_time
greater than 780.
To do that, you will have to manually filter out the objects using list comprehensions.
# perform filtering and get the first 'A' object
obj = A.objects.filter(name__exact='xyz',slot_hours__start_time__gte=780)[0]
# get the relavent 'SlotTime' objects
slot_time_objs = [x for x in obj.slot_hours if x.start_time >= 780]
Upvotes: 0