Mongoengine: Greater than (gte) query in MongoEngine for EmbeddedDocumentListField

Created on 10 May 2016  路  4Comments  路  Source: MongoEngine/mongoengine

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 something similar 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

Any idea, how to do this?

Question

Most helpful comment

I'll try writing a test for it. Let's see if it's supported.

All 4 comments

Pymongo supports this, refer this: http://stackoverflow.com/a/17613022/1647035
The catch here is in case you need the complete parent document and specific documents from the EmbeddedDocumentListFields then this becomes slightly messy.
@nilay-gpt the query in your case can be something like the following:

db.coll.find(
                { 
                    'slot_hours. start_time': {
                                                    $gte: 780
                                              } 
                }, 
                {
                    'slot_hours.$': 1, 
                    'name':1
                }
            );

I'll try writing a test for it. Let's see if it's supported.

Assuming you have the schema that you provided, you should be able to 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)

(Posted on SO as well)

I'm closing this as the issue is quite old and there is no obvious bug

Was this page helpful?
0 / 5 - 0 ratings