【问题标题】:storing a special character in mongodb using pymongo使用 pymongo 在 mongodb 中存储特殊字符
【发布时间】:2021-01-05 07:47:53
【问题描述】:

我有一个 Json 文件,其中包含如下 json 列表:

[{"name":"somename","value":{"$ne":"negativity"}}]

我正在解析或读取此文件并转储到 mongo 中,例如:

for file in os.listdir('/opt/myfiles/test.json'):
    with open(file, 'r') as fi:
        dict = json.load(fi)
        for data in dict:
            db.collection.insert(data)

它会抛出一个错误:

bson.errors.InvalidDocument: key '$ne' must not start with '$'

【问题讨论】:

    标签: python mongodb pymongo bson robo3t


    【解决方案1】:

    驱动程序不支持在字段名称中使用 $。见https://docs.mongodb.com/manual/core/document/#field-names

    根据那里给出的规则,您应该能够将文档插入服务器,因为 $ne 不是顶级字段,只要您找到允许您执行此操作的驱动程序(pymongo 显然不是其中之一他们)。

    【讨论】:

    • 新版本更好吗?
    猜你喜欢
    • 1970-01-01
    • 2014-12-20
    • 2023-03-26
    • 2015-06-14
    • 1970-01-01
    • 2021-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多