MongoEngine / MongoEngine/mongoengine

Never writing default values in DB if key not present

Open
#1,756 9 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Model Migration
Dominant language
Python
Stars
4.3k
Forks
1.2k
Avg merge
4h 41m
Merged PRs (30d)
11

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the Python reproducer against MongoDB and compare the document before and after the external $unset and save calls. Trace the save and reload entry points involved in EmbeddedDocumentField and default values. Done means the reproduced missing embedded default is persisted, with regression coverage for the shown sequence.

Written by the indexing model from the issue text.

Assessment

Tech stack
mongodb, python
Domain
backend, database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.