Home > Backend Development > Python Tutorial > Introduction to Python's method of saving files on MongoDB locally

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

高洛峰
Release: 2017-03-07 15:43:02
Original
2162 people have browsed it

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())
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template