MongoEngine / MongoEngine/mongoengine

Dict field in mongoengine does not save None types

Open
#2,051 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

When adding a new field to Dict type if the value is None key is never saved.
Attaching the code snippet below

import mongoengine
from mongoengine import *

class Test(Document):
    args = DictField()

test = Test(args={'hello': None})
test.save()
#create a new key count with None
test.args['count'] = None
test.save()
for item in Test.objects:
    print(item.args)

Output:

connected
{'hello': None}

Count with None is never saved !

But when doing this:

import mongoengine
from mongoengine import *

class Test(Document):
    args = DictField()

test = Test(args={'hello': None})
test.save()
test.args['count'] = '1'
test.save()
for item in Test.objects:
    print(item.args)

Output:

connected
{'hello': None, 'count': '1'}

When count is a non-None value it is saved

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 provided Python reproduction with DictField and compare saving a newly added None value with a non-None value. Trace how the DictField contents are persisted during the second save. Done means a newly added key whose value is None is present after saving and reading the document again.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.