我如何在猫鼬中生成 child 模型
How can i generate child model in mongoose
我正在尝试对以下内容进行建模。
我有一个名为 Brick
的 parent model
,它具有一些属性。
将有 5 种以上的砖块,它们都具有自己需要的特定属性。
我希望以后能够 select 某个客户 ID 的所有 Brick,无论类型(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 },
}));
child 的示例是 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 应该继承 Brick 的属性,但我不知道如何..
你能帮我找到正确的方向吗?
谢谢!
史蒂文
只需将 Brick 模型添加为属性(组合)即可。
它将弥补这一点。
或者仅仅依靠现有的 mongoose 插件 https://github.com/briankircho/mongoose-schema-extend
检查这个。
那是因为您没有 "require" 之前的文件 ,所以从技术上讲它超出了范围并且 TwitterWallBrickSchema 不知道什么是 "BrickSchema".
要么将两个模型放在同一个文件中,要么将第一个文件放在第二个文件中.
我的解决方案是在 brickSchema 中设置一个新的 "content" 字段,并拆分为不同的文件:
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
的 parent model
,它具有一些属性。
将有 5 种以上的砖块,它们都具有自己需要的特定属性。
我希望以后能够 select 某个客户 ID 的所有 Brick,无论类型(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 },
}));
child 的示例是 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 应该继承 Brick 的属性,但我不知道如何.. 你能帮我找到正确的方向吗?
谢谢! 史蒂文
只需将 Brick 模型添加为属性(组合)即可。 它将弥补这一点。 或者仅仅依靠现有的 mongoose 插件 https://github.com/briankircho/mongoose-schema-extend 检查这个。
那是因为您没有 "require" 之前的文件 ,所以从技术上讲它超出了范围并且 TwitterWallBrickSchema 不知道什么是 "BrickSchema". 要么将两个模型放在同一个文件中,要么将第一个文件放在第二个文件中.
我的解决方案是在 brickSchema 中设置一个新的 "content" 字段,并拆分为不同的文件:
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');
希望对您有所帮助!