我有这样一个字符串:
document = '{ time : 14/09/19 16:00:00,
global : { full:190 , defects: 7 , btp: 6 , total: 202 } ,
domains : [ { domain : "A" , full:7 , defects: 2 , btp: 0 , total: 9 },
{ domain : "B" , full:0 , defects: 0 , btp: 0 , total: 0 },
{ domain : "C" , full:6 , defects: 0 , btp: 0 , total: 6 },
{ domain : "D" , full:26 , defects: 0 , btp: 2 , total: 28 },
{ domain : "E" , full:0 , defects: 0 , btp: 0 , total: 0 },
{ domain : "F" , full:4 , defects: 0 , btp: 2 , total: 6 },
{ domain : "G" , full:0 , defects: 0 , btp: 0 , total: 0 },
{ domain : "H" , full:21 , defects: 0 , btp: 1 , total: 22 },
{ domain : "I" , full:32 , defects: 0 , btp: 0 , total: 32 },
] }'
当我尝试将其插入到 mongoDB 集合中时,出现以下错误:
TypeError: 'str' object does not support item assignment
我不知道如何将字符串转换为 BSON,以便用它执行插入。
我查看了 bson_util
模块,但没有成功。
最佳答案
这里重要的不是 BSON 部分。 MongoDB 驱动程序会处理这些。您的工作是将 JSON 转换为有效的 Python 数据结构:
import json
data = json.loads(document)
collection.insert(data);
关于python - 如何修复 "' str' object does not support item assignment"在 MongoDb 中的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28898476/