Home  >  Article  >  Backend Development  >  Introduction to Python’s method of saving files on MongoDB locally

Introduction to Python’s method of saving files on MongoDB locally

高洛峰
高洛峰Original
2017-03-07 15:43:022100browse

The example in this article describes how Python saves files on MongoDB locally. Share it with everyone for your reference, the details are as follows:

Documents on MongoDB are operated through GridFS. Python can also connect to the MongoDB database through pymongo and use the gridfs method of the pymongo module to operate documents. The following example saves an excel document stored in GridFS on MongoDB locally.

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())

For more Python methods to save files on MongoDB locally, please pay attention to the PHP Chinese website for related articles!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn