2017-03-24 3 views
0

を使用して分割するサブ文書私は4つのタイプに分けることができるモデルAttachment、持っている場合:LinkYoutubeVideoGoogleDriveFile、および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に設定するとエラーが表示されます。スキーマをネストしようとしましたか?参照や配列を使用してのみネストできます。

このエラーが何を意味するのか分かりませんが、これを行う方法を説明するドキュメントは見つかりません。助けて?

+1

役立つかもしれない:http://thecodebarbarian.com/mongoose-4.8-embedded-discriminators.html – mjoyce91

答えて

0

は、以下を実行してみてください。

var AttachmentSchema = new Schema({ 
    id:  {type: String, required: true}, 
    title: {type: String, required: true} 
    }); 

var PostSchema = new Schema({ 
    text: { type: String }, 
    attachments: [ AttachmentSchema ] // Could be Material.Link, Material.YoutubeVideo, etc 
}); 

var attachmentArray = PostSchema.path('attachments'); 

    module.exports = { 
    Post: mongoose.model('Post', PostSchema), 
    DriveFile:  attachmentArray.discriminator('GoogleDriveFile', new mongoose.Schema({})), 
    DriveFolder: attachmentArray.discriminator('GoogleDriveFolder', new mongoose.Schema({})), 
    Link:   attachmentArray.discriminator('Link', new mongoose.Schema({})), 
    YoutubeVideo: attachmentArray.discriminator('YoutubeVideo', new mongoose.Schema({})) 
}; 

キーはあなたの弁別のためのベースとして、親文書スキーマのschema.pathを使用マングースモデルを使用しないことです。

このリンク上の用語 docArrayため

検索:Mongoose Discriminator documentation