1

I want to store files in MongoDB using GridFS for different purposes like streaming videos and saving images. This ones are part of one mongoose document "Article".

Im thinking on using different buckets from GridFS for each file type "videos" and "thumbnails" but Im not sure how to create the Schema and the Model to populate the entire collection.

I have this schema and i want to populate with the fs.files in each video and thumbnail.

var mongoose    = require('mongoose'),
    Schema      = mongoose.Schema;

var ArticleSchema   = new Schema({
    title       : { type : String, required : true },
    description : { type : String, required : true },
    votes       : { type : Number, default : 0 },
    createdAt   : { type : Date, default : Date.now },
    // ObjectID of the fs.file video
    // ObjectID of the fs.file thumbnail
});

ArticleSchema.pre('save', function(next) {
    store this files using here the createWriteStream
});

module.exports = mongoose.model('Article', ArticleSchema);

Question:

The logic to store this collection using 'pre' method must be in the model?

I see a loot of examples and related questions here, but all put the logic of storing the files in the controller instead of doing it in the model.

Mateo Marconi
  • 614
  • 5
  • 16

0 Answers0