我有一个像这样的字符串:
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'对象不支持项目分配
我不知道如何将字符串转换为BSON以便执行插入操作。
我查看了bson_util
模块,但没有运气。
在这里,重要的不是BSON部分。MongoDB驱动程序将解决此问题。您的工作是将JSON转换为有效的python数据结构:
import json
data = json.loads(document)
collection.insert(data);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句