首页 / MONGODB / mongodb操作文件
mongodb操作文件
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了mongodb操作文件,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1984字,纯文字阅读大概需要3分钟。
内容图文
When to Use GridFS
In MongoDB, use GridFS for storing files larger than 16 MB.
=======================
‘‘‘ Created on 2013-8-6 class mongoInsert @author: tree ‘‘‘ __metaclass__ = type import os from pymongo.database import Database import time import gridfs class mongoImg(object): """mongoInsert is a class for inserting document """ def __init__(self, database, dir): """Create a new instance of :class:mongoInsert :Parameters: - `database`: database to use - `dir` : directory of document """ if not isinstance(database, Database): raise TypeError("database must be an instance of Database") if len(dir) < 1: raise TypeError("dir must be an string of directory") # self.__con = Connection() self.__imgdb = database self.__imgfs = gridfs.GridFS (self.__imgdb) self.__dir = dir self.__filelist=[] #save filepath in list.txt def __dirwalk(self,topdown=True): """traverse the documents of self.__dir and save in self.__filelist """ sum=0 self.__filelist.clear() for root,dirs,files in os.walk(self.__dir,topdown): for name in files: sum+=1 temp=os.path.join(root,name) self.__filelist.append(temp) print(sum) #insert image def insert(self): """insert images in mongodb """ self.__dirwalk() tStart = time.time() for fi in self.__filelist: with open (fi,‘rb‘) as myimage: data=myimage.read() self.__imgfs.put(data, content_type = "jpg", filename =fi) tEnd =time.time () print ("It cost %f sec" % (tEnd - tStart)) #get image by filename def getbyname(self,filename,savepath): """get img from mongdb by filename """ if len(savepath) < 1: raise TypeError("dir must be an string of directory") dataout=self.__imgfs.get_version(filename) try: imgout=open(savepath,‘wb‘) data=dataout.read() imgout.write(data) finally: imgout.close()
mongodb操作文件
标签:pat mongo finally def root nts intern try document
本文系统来源:https://www.cnblogs.com/testzcy/p/9802152.html
内容总结
以上是互联网集市为您收集整理的mongodb操作文件全部内容,希望文章能够帮你解决mongodb操作文件所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。