Skip to content

Unable to set null to field  #2344

Description

@MaXXXXfeng

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.

Activity

  1. bagerard commented on Jun 20, 2020

    @bagerard
    Collaborator

    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.

  2. ceorcham commented on Jun 22, 2020

    @ceorcham

    @bagerard can I give a try on this one?

  3. rykroon commented on Jan 16, 2021

    @rykroon

    @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

  4. stakaz commented on Jul 27, 2026

    @stakaz

    Hello, des someone has an Ide how to fix it? using pymongo directly is for sure an option, but for me it would mean to rewrite a bunch of code. Ideally, I would like to have something like: if null=True for a field is set and None is provided as value, save it with the mongodb "null", otherwise skip.

  5. bagerard commented on Sep 2, 2026

    @bagerard
    Collaborator

    FYI This has normally been addressed on master (upcoming 1.0) via #2928 , closing this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions