How to create mongoose model for GridFS collection?
Late response but try replacing ref: 'tracks.files'
with ref: 'trackSchema'
.
The ref
field is referenced within populate('trackSchema')
and must be a reference to another Schema. Check saving-refs out if you want to learn more about populating fields and references in Mongoose.
I also wouldn't recommend creating schemas of any sort for implementing GridFS, I'd let Mongo take care of this as it may lead to file corruption or missing/outdated documents if implemented incorrectly.
As for the lack of official documentation for GridFS, especially GridFS with Mongoose, I used Mongo's native GridFsBucket class (as of my knowledge, no still maintained official Mongoose-GridFS API exist), the best docs I used when attempting this myself was gridfs. With a tutorial here streaming.
To use Mongo's native GridFSBucket with Mongoose just grab its mongo
property and Mongoose's connection instance from its connection
property.
const mongoose = require(mongoose);var gridFSBucket = new mongoose.mongo.GridFSBucket(mongoose.connection.db, { bucketName: 'images'});