如何将json文件转储到mongodb?

时间:2014-07-08 06:34:38

标签: python json mongodb

我有几百个JSON文件要插入到mongo数据库中。我写了这段代码,但它没有用。我收到错误

TypeError: 'str' object does not support item assignment

代码如下:

import glob
from pymongo import MongoClient

client = MongoClient('localhost',27017)
path = '/home/rahul/Desktop/*.json'
files = glob.glob(path)
db = client.helloworld
c = db.docs
for f in files:
    c.insert(f)

1 个答案:

答案 0 :(得分:0)

insert接受dict格式的参数

for f in files:
    c.insert({"file_name":f})

查看docs