Ananth.P
Ananth.P

Reputation: 445

Can't use "gte" in mongoengine filter query

I have a MongoEngine Document class Student..

class Student(Document):
    db_id = StringField()
    name = StringField()
    score = IntField()
    deleted = BooleanField(default=False, required=True)

I would like to query as

Student.objects.filter(score__gte=30)

But I'm getting a error like AttributeError: 'int' object has no attribute 'get'

Can someone please help me in how I can do this? Thank you!

Upvotes: 0

Views: 174

Answers (1)

bagerard
bagerard

Reputation: 6354

The following (minimal) snippet works fine

from mongoengine import *
connect()

class Student(Document):
    name = StringField()
    score = IntField()

Student(name='Bob', score=35).save()

print(Student.objects(score__gte=30))
# output: [<Student: Student object>]

I don't have any problem to run your code, perhaps start from mine and build on top until you identify the culprit. I would also recommend to drop the existing mongo collection before you test this.

In fact, depending on where the error is fired (we don't have the stack trace so we can't tell), it could be that it fails when loading the existing mongo document (returned by your query) into the Student constructor

Upvotes: 1

Related Questions