将mongoengine中的字段更新为null

Update field to null in mongoengine

我正在使用 mongoengine。我希望能够在数据库中将我的字段设置为空,例如。字符串为空。

我尝试了以下方法,在我看来,每当我通过 None 时,该字段都会被跳过,我无法替换现有值。 这甚至可能吗?

if user.contact_phone_number == "":
    user.contact_phone_number = None

我的文档:

contact_phone_number = StringField(
    db_field="contactPhoneNumber",
    regex=RE_CONTACT_PHONE,
    unique=False,
    sparse=True,
    required=False,
    null=True
)

将 StringField 设置为 None 没有任何问题,请参阅下文

from mongoengine import *

connect()

class TestDoc(Document):
    s = StringField()

TestDoc(s='test').save()

t = TestDoc.objects.first()
assert t.s == 'test'

t.s = ""
t.save()

t = TestDoc.objects.first()
assert t.s is ""

t.s = None
t.save()

t = TestDoc.objects.first()
assert t.s is None