我怎样才能生成mongoose的孩子模型

我试图模拟以下。

我有一个叫做Brickparent model ,它有一些属性。 将有5 +砖,将都有自己的具体属性,这也是需要的。

我希望以后能够select某个客户ID的所有砖块,无论types是什么(TwitterBrick,FacebookBrick等等)。

 var mongoose = require('mongoose'); var Schema = mongoose.Schema; // set up a mongoose model module.exports = mongoose.model('Brick', new Schema({ type: String, userid: { type: String, required: true}, animationspeed: { type: Number, min: 1, max: 100 }, socialsite: String, // none, twitter, instagram socialsearchtags: String, tagline: { type: String, minlength:3,maxlength: 25 }, })); 

一个孩子的例子是TwitterBrick 。 现在是:

 var mongoose = require('mongoose'); var Schema = mongoose.Schema; module.exports = mongoose.model('TwitterBrick', new Schema({ bgColor1: String, bgColor2: String, bannerBgColor1: String, bannerBgColor2: String, })); 

TwitterBrick应该inheritance砖的属性,但我不知道如何..你能帮助我在正确的方向吗?

谢谢! 史蒂芬

我的解决scheme是在brickSchema中设置一个新的“内容”字段,并分割成不同的文件:

brick.schema.js

 var mongoose = require('mongoose'); module.exports = { type: String, userid: { type: String, required: true}, animationspeed: { type: Number, min: 1, max: 100 }, socialsite: String, // none, twitter, instagram socialsearchtags: String, tagline: { type: String, minlength:3,maxlength: 25 }, content: {type:mongoose.Schema.Types.Mixed, required: false, default: null} } 

brick.model.js

 var mongoose = require('mongoose'); var Schema = mongoose.Schema; var BrickSchema = new Schema(require('brick.schema.js')); module.exports = mongoose.model('defaultBrick', BrickSchema, 'Bricks'); 

twitterBrick.model.js

  var mongoose = require('mongoose'); var Schema = mongoose.Schema; var brickSchema = require('brick.schema.js'); brickSchema.content = new Schema({ bgColor1: String, bgColor2: String, bannerBgColor1: String, bannerBgColor2: String, }); var BrickSchema = new Schema(require('brick.schema.js')); module.exports = mongoose.model('twitterBrick', BrickSchema, 'Bricks'); 

希望能帮助到你 !

只需添加Brick模型作为一个属性(组合)。 它会弥补这一点。 或者只是依靠现有的mongoose插件为该https://github.com/briankircho/mongoose-schema-extend检查这一个。

那是因为你没有“要求”以前的文件 ,所以在技术上它超出了范围,TwitterWallBrickSchema没有线索什么是“BrickSchema”。 要么你把两个模型放在同一个文件中,要么在第二个文件中需要第一个文件