使用如下代码对mongodb进行文件的存入与读取
from pymongo import MongoClient
from gridfs import *
# 存入mongodb
#链接mongodb
client=MongoClient('localhost',27017)
#取得对应的collection
db=client.pdf
datatmp = open('test.pdf', 'rb')
imgput = GridFS(db)
insertimg=imgput.put(datatmp,content_type='pdf',filename='test',is_do=0)
datatmp.close()
print("js")
#读取mongodb
gridFS = GridFS(db)
count=0
for grid_out in gridFS.find({'is_do':0}):
count+=1
print(count)
print(grid_out.filename)
data = grid_out.read() # 获取图片数据
outf = open('test2.pdf','wb')#创建文件
outf.write(data) # 存储图片
outf.close()
print('end')