This article mainly introduces how to save MongoDB files to a local place using Python, and describes how to use Python to call the gridfs method of the pymongo module to operate MongoDB files, for more information about how to save MongoDB files to a local machine, see the example in this article. We will share this with you for your reference. The details are as follows:
The 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 the documents. The following example saves the excel file stored in GridFS on MongoDB to a local device.
From pymongo import export clientimport gridfsclient = MongoClient ('mongodb: // username: pwd@192.168.1.22: 27017/send_excel ') db = client. js_send_excelfs = gridfs. gridFS (db) files = fs. find () print ('total: ', 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 information about how to save MongoDB files to a local machine in Python, refer to PHP!