使用mongoDB的gridfs模块存储大于16m的文档
1. 使用gridfs
from pymongo import MongoClient
from gridfs import GridFS
mongo_url='mongodb://xx:x@127.0.0.1:27017/xxx?authSource=admin'
client = MongoClient(mongo_url)
db=client['mycol']
collection=db['collection_name']
gdb=GridFS(db,collection=collection.name)
data_dict={}
gdb.put(dict_json,filename=str(file_id))
data_dict=gdb.get(file_id).read()
obj_id=.collection.files.find_one({'filename':file_id})['_id']
del_chunks_result=collection.chunks.delete_many({'files_id':obj_id})
del_file_result=collection.files.delete_one({'_id':obj_id})
if del_chunks_result.deleted_count==125 and del_file_result.deleted_count==125:
log.log(1, "Delete file {} successfully.".format(file_id))
self.collection.files.drop()
self.collection.chunks.drop()