Mongoengine: Unable to set null to field

Created on 5 Jun 2020  路  3Comments  路  Source: MongoEngine/mongoengine

I created a document A.
A.field1= None
A.save().
But when I try to save the document to MongoDB, the keys with a "None" value are not inserted into the collection. The field1 disappear.

Most helpful comment

Right this is an historical decision that was taken a while ago but that I'd like to change at some point. When you set the field to None, it is removed from the document instead of being actually set to None.

I guess it was done to save a bit of space in MongoDB but I think it was a bad design decision and I'd like to change that in a near future.

All 3 comments

Right this is an historical decision that was taken a while ago but that I'd like to change at some point. When you set the field to None, it is removed from the document instead of being actually set to None.

I guess it was done to save a bit of space in MongoDB but I think it was a bad design decision and I'd like to change that in a near future.

@bagerard can I give a try on this one?

@MaXXXXfeng
@bagerard

To add to this, I feel like if the Field has null=False and does not have a default value, then the default value should be the "zero value" of that type.
for example:
StringField defaults to ""
IntField defaults to 0
BooleanField defaults to False
etc

Was this page helpful?
0 / 5 - 0 ratings