首页 > 解决方案 > Retrieve file from mongoDB using python

问题描述

I have doc file stored in mongodb. I want to retrieve stored doc file on mongoDB using python. Any suggestion will really be helpful. I'm unable to find article to move forward. All I could find is uploading file through python and retrieving through python. But I want to retrieve file stored on mongoDB which is not created on python but retrieving through python. P.S.: I stored file on mongodb gridfs using express js.

Upload.JS:: JS used to upload file on mongodb gridfs .

const util = require("util");
const multer = require("multer");
const GridFsStorage = require("multer-gridfs-storage");
const mongoose = require('mongoose');
const Grid = require('gridfs-stream');
const mongoURI ='mongodb://localhost:27017/file_uploaded_new';
const promise = mongoose.connect(mongoURI, { useNewUrlParser: true });
const conn = mongoose.connection;
let gfs;
conn.once('open',() => {
  gfs = Grid(conn, mongoose.mongo);
  gfs.collection('uploads');
});
var storage = new GridFsStorage({
    db: promise,
    options: {useNewUrlParser: true, useUnifiedTopology: true },
    file: (req, file) => {
        return new Promise((resolve, reject) => {
            if (file.mimetype === 'application/vnd.openxmlformats- 
officedocument.wordprocessingml.document') {
                resolve({
                    bucketName: 'words',
                    filename: `${file.originalname}`
                })
            } else {
                reject(Error("File type has been rejected"));
            }
        });    
    }
});
var uploadFile = multer({storage: storage}).single("file");
var uploadFilesMiddleware = util.promisify(uploadFile);
module.exports = uploadFilesMiddleware;

words.chunk file in MongoDB

Words.file in MongoDB

I have successfully uploaded docx file in MongoDB. How can I retrieve the uploaded word file using python script?

标签: pythonmongodbexpresspymongogridfs

解决方案


推荐阅读