AddPdfToMongoDB

    0

    0

    YusifHasanov

    This creates a new Mongoose GridFS model, stores a file in that model and then writes the file to a given path.

    const mongoose = require('mongoose');
    const Grid = require('mongoose-gridfs');
    
    const conn = mongoose.createConnection(uri, { useNewUrlParser: true });
    const gridfs = new Grid(conn.db, mongoose.mongo);
    
    const storage = gridfs.model;
    
    const pdf = new storage({
      filename: 'file.pdf',
      contentType: 'application/pdf',
    });
    pdf.write(fs.createReadStream('path/to/file.pdf'), (error) => {
      if (error) {
        console.log(error);
      } else {
        pdf.save((error) => {
          if (error) {
            console.log(error);
          } else {
            console.log('file saved');
          }
        });
      }
    });
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.