0

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!

bagerard
  • 5,681
  • 3
  • 24
  • 48
Ananth.P
  • 445
  • 2
  • 8

1 Answers1

1

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

bagerard
  • 5,681
  • 3
  • 24
  • 48
  • Thanks for the reply – Ananth.P Jan 24 '20 at 16:55
  • If the score will be list field how can i calculate score array must have grater than 0? – Ananth.P Jan 24 '20 at 16:56
  • If `scores = ListField(IntField())` and you want to return all students with at least 1 score > 0, you need to use `Student.objects(scores__gte=0)`. If you need to query on the sum of the score, then you need to use an `aggregation` pipeline but that's more complex – bagerard Jan 25 '20 at 09:23
  • yes ,it working but if field will be like scores=ListField(embeddedDocumentFIeld(Scores)),then how can i calculate scores field greater than 0 ? – Ananth.P Jan 25 '20 at 12:06
  • This deviates from the initial question and StackOverflow's comment isnt the best mean of communications. Please consider accepting/upvoting the answer if it helped you. In case you want additional support, open an issue in the github project (https://github.com/MongoEngine/mongoengine/issues) and make sure to add your 2 corresponding classes (`class Student(Document)` and `class Scores(EmbeddedDocument)`) in the description. I'm a contributor of MongoEngine so I'll help you there – bagerard Jan 25 '20 at 20:42