Skip to content

Never writing default values in DB if key not present #1756

Open
@sknat

Description

@sknat

Default values are never written to DB if containing prop was removed before:

import mongoengine

class Embdoc(mongoengine.EmbeddedDocument):
    prop = mongoengine.IntField(default=0)

class Doc(mongoengine.Document):
    emb = mongoengine.EmbeddedDocumentField(Embdoc, default=Embdoc)

    meta = {
        "db_alias": "db_name",
        "collection": "collection"
    }

mongoengine.connection.register_connection('db_name',name='db_name',host='localhost')
collection = mongoengine.connection.get_db('db_name').collection

collection.remove()
o = Doc()
o.save()
print(o.emb.prop)                             # 0
print(collection.find().limit(1)[0])          # {u'_id': ObjectId('5abaaaaaaaaaaaaaaaaaaaaa'), u'emb': {u'prop': 0}}
collection.update({}, {"$unset": {"emb":1}})
o.reload()
o.save()
print(o.emb.prop)                             # 0
print(collection.find().limit(1)[0])          # {u'_id': ObjectId('5abaaaaaaaaaaaaaaaaaaaaa')}

# Even so
o.emb.prop = 0
o.save()
print(o.emb.prop)                             # 0
print(collection.find().limit(1)[0])          # {u'_id': ObjectId('5abaaaaaaaaaaaaaaaaaaaaa')}

Funny isn't it ?

python -c "import mongoengine; print(mongoengine.__version__)" -> 0.15.0
ubuntu 17.10 artful
kernel 4.13.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Model MigrationIssues about Document schema migration

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions