码迷,mamicode.com
首页 > 数据库 > 详细

mongodb操作文件

时间:2018-10-17 11:05:16      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:pat   mongo   finally   def   root   nts   intern   try   document   

mongodb操作文件,主要是通过GridFS类。存储文件主要存放在fs中,其中的fs是数据库默认的。并且GridFS是直接与数据库打交道,与collection集合无关。

 ===============

https://docs.mongodb.com/manual/core/gridfs/

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!