Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoengine: mongoengine.errors.FieldDoesNotExist

I've been confused with this error. I'm creating a Vehicle database with mongoengine that goes like this.

class Vehicles(Document):
    make: StringField()
    model: StringField()
    license: StringField()
    year: IntField()
    mileage: IntField()
    deleted: BooleanField()

for j in range(2):
    vehicle = Vehicles(
        make="Honda",
        model="Civic",
        license="H54 " + str(i) + str(j),
        year=2015,
        mileage= 500 + (i * 500),
        deleted=False
    ).save()

While I try running the code, I've been getting:

mongoengine.errors.FieldDoesNotExist: The fields "{'license', 'year', 'model', 'mileage', 'make', 'deleted'}" do not exist on the document "Vehicles"

I don't understand why it's giving me this error. I know a solution is to change Document into DynamicDocument, but I don't really see why. Can someone please explain to me this error???

like image 526
DasNoob Avatar asked Jan 20 '26 09:01

DasNoob


1 Answers

You are writing your class as if it was a dataclass but Mongoengine has no support for type hinting so it is not intepreting that correctly. MongoEngine is in fact currently not seeing any of your field definitions.

Turn your ":" into "=" and it should be fine

class Vehicles(Document):
    make = StringField()    # instead of 'make : StringField()'
    ...
like image 56
bagerard Avatar answered Jan 23 '26 00:01

bagerard