Python保存MongoDB上的文件到本地的方法,


本文实例讲述了Python保存MongoDB上的文件到本地的方法。分享给大家供大家参考,具体如下:

MongoDB上的文档通过GridFS来操作,Python也可以通过pymongo连接MongoDB数据库,使用pymongo模块的gridfs方法操作文档。以下示例是把MongoDB上GridFS存的excel文档保存到本地。

from pymongo import MongoClient
import gridfs
client = MongoClient('mongodb://username:pwd@192.168.1.22:27017/send_excel')
db = client.js_send_excel
fs = gridfs.GridFS(db)
files = fs.find()
print('总数:', files.count())
for ffle in files:
  if ffle.filename.find('.xls') > 0:
    with open(ffle.filename, 'wb') as f1:
      f1.write(ffle.read())

转自:小谈博客 http://www.tantengvip.com/2015/07/python-mongodb-save-file/

希望本文所述对大家Python程序设计有所帮助。

您可能感兴趣的文章:

  • Python简单连接MongoDB数据库的方法
  • python&MongoDB爬取图书馆借阅记录
  • Python使用MONGODB入门实例
  • python实现连接mongodb的方法
  • Python操作MongoDB数据库PyMongo库使用方法
  • 使用Python脚本操作MongoDB的教程
  • Python中的MongoDB基本操作:连接、查询实例
  • Python中使用Flask、MongoDB搭建简易图片服务器
  • python连接mongodb操作数据示例(mongodb数据库配置类)

评论关闭