So I am trying to create a mongoose model for GridFS collection but to no success.
let bucket;
(async () => {
try {
await mongoose.connect(process.env.MONGODB_URI, { useNewUrlParser: true });
const { db } = mongoose.connection;
bucket = new mongoose.mongo.GridFSBucket(db, { bucketName: 'tracks' });
}
catch(err) {
console.log(err);
}
})();
Here is my course schema and model:
const courseSchema = new mongoose.Schema({
name: {
type: String,
required: true,
},
tracks: [{
type: mongoose.Types.ObjectId,
ref: 'tracks.files'
}],
});
const Course = mongoose.model('Course', courseSchema);
And here is my tracks schema and model:
const trackSchema = new mongoose.Schema({
length: { type: Number },
chunkSize: { type: Number },
uploadDate: { type: Date },
filename: { type: String, trim: true, searchable: true },
md5: { type: String, trim: true, searchable: true },
}, { collection: 'tracks.files', id: false });
const Track = mongoose.model('Track', trackSchema);
And I am getting this error:
MongooseError [MissingSchemaError]: Schema hasn't been registered for model "tracks.files".
when I run this:
Course.findById('5d5ea99e54fb1b0a389db64a').populate('tracks').exec()
.then(test => console.log(test))
.catch(err => console.log(err));
There is absolutely zero documentation on these stuff and I am going insane over it. Am I the first person to ever save 16 MB+ files in Mongodb? Why is it so difficult to implement this? Can anyone please guide me to the right direction.