【问题标题】:mongoEngine miss the filed when the value is null after savemongoEngine在保存后值为null时错过了该字段
【发布时间】:2020-09-24 04:31:12
【问题描述】:

这是一个示例文档:

{"id":1,
"info":{"age":10,"grade":80,"address":"LA"}
}

我做了什么:

# query the document
student = Student.objects(id=1).first()
# change the info
student.info['address'] = None
# save the change
studen.save()

当我查看数据库时,该文档缺少地址字段。信息是 {"age":10,"grade":80} 为什么 save() 操作会自动错过空字段,如何避免? (我知道使用 update 可以保留 null 字段,但是我可以使用 save 仍然保留 null 字段吗?)


【问题讨论】:

    标签: python mongodb pymongo mongoengine flask-mongoengine


    【解决方案1】:

    您可以尝试给它Nan 或 void string ""

    student.info = {"age":10,"grade":80,"address":""}
    

    student.info = {"age":10,"grade":80,"address":Nan}
    

    【讨论】:

    • 那么没有什么好的方法可以让address的值保持为None吗?
    • @McCreeFeng ,在 Python 中不,我们使用 Nan 代替
    猜你喜欢
    • 2020-03-05
    • 2016-10-31
    • 1970-01-01
    • 1970-01-01
    • 2018-06-28
    • 2021-05-17
    • 1970-01-01
    • 2012-05-30
    • 1970-01-01
    相关资源
    最近更新 更多