【发布时间】:2017-08-16 19:52:38
【问题描述】:
如果我有一个模型Attachment,可以分为4种类型:Link、YoutubeVideo、GoogleDriveFile和GoogleDriveFolder,如何使用Mongoose将Attachment区分为这些类型,并允许它们成为另一个模式中的子文档; Post?
我创建了基础Attachment 模型,并使用鉴别器将其划分为单独的模型:
var AttachmentSchema = new Schema({
id: {type: String, required: true},
title: {type: String, required: true}
});
var Attachment = mongoose.model('Material', AttachmentSchema);
module.exports = {
DriveFile: Attachment.discriminator('GoogleDriveFile', new mongoose.Schema()),
DriveFolder: Attachment.discriminator('GoogleDriveFolder', new mongoose.Schema()),
Link: Attachment.discriminator('Link', new mongoose.Schema()),
YoutubeVideo: Attachment.discriminator('YoutubeVideo', new mongoose.Schema())
};
现在,在Post 架构中,应该有一个附件数组,具有不同的类型:
var Attachment = require('./attachment');
var PostSchema = new Schema(
text:{type: String},
attachments: [Material] // Could be Material.Link, Material.YoutubeVideo, etc
});
执行此操作时,我收到一条错误消息,提示“GoogleDriveFile 处的未定义类型 Model。您是否尝试嵌套架构?您只能使用引用或数组进行嵌套。”
我不知道这个错误是什么意思,也找不到任何解释如何执行此操作的文档。帮忙?
【问题讨论】: