我试图为GridFS集合创建一个mongoose模型,但是一直没有成功。
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);
}
})();
以下是我的课程架构和模型:
const courseSchema = new mongoose.Schema({
name: {
type: String,
required: true,
},
tracks: [{
type: mongoose.Types.ObjectId,
ref: 'tracks.files'
}],
});
const Course = mongoose.model('Course', courseSchema);
这是我的曲目模式和模型:
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);
我遇到了这个错误:
MongooseError [MissingSchemaError]: Schema hasn't been registered for model "tracks.files".
当我运行这个程序时:
Course.findById('5d5ea99e54fb1b0a389db64a').populate('tracks').exec()
.then(test => console.log(test))
.catch(err => console.log(err));
这些东西完全没有文档,我对此感到非常疯狂。我是第一个在Mongodb中保存超过16 MB的文件吗?为什么实现这个如此困难?有人能否请指导我正确的方向。