を使用して分割するサブ文書私は4つのタイプに分けることができるモデルAttachment
、持っている場合:Link
、YoutubeVideo
、GoogleDriveFile
、およびGoogleDriveFolder
、どのように私はこれらの種類に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
に設定するとエラーが表示されます。スキーマをネストしようとしましたか?参照や配列を使用してのみネストできます。
このエラーが何を意味するのか分かりませんが、これを行う方法を説明するドキュメントは見つかりません。助けて?
役立つかもしれない:http://thecodebarbarian.com/mongoose-4.8-embedded-discriminators.html – mjoyce91