[英]Mongoose Schema: Forcing an array of objects to be created
我的貓鼬模式如下所示:
var userSchema = mongoose.Schema({
fbid : String,
googleid : String,
birthday : String,
email : String,
first_name : String,
last_name : String,
gender : String,
age : Number,
location : String,
paid : { type: Boolean, default: 0 },
lessons: [
{
name : { type: String, default: 'foobar1'},
sent : { type: Boolean, default: 0 },
read : { type: Boolean, default: 0 },
activity: { type: Boolean, default: 0 }
},
{
name : { type: String, default: 'foobar2'},
sent : { type: Boolean, default: 0 },
read : { type: Boolean, default: 0 },
activity: { type: Boolean, default: 0 }
},
{
name : { type: String, default: 'foobar3'},
sent : { type: Boolean, default: 0 },
read : { type: Boolean, default: 0 },
activity: { type: Boolean, default: 0 }
},
{
name : { type: String, default: 'foobar4'},
sent : { type: Boolean, default: 0 },
read : { type: Boolean, default: 0 },
activity: { type: Boolean, default: 0 }
},
{
name : { type: String, default: 'foobar5'},
sent : { type: Boolean, default: 0 },
read : { type: Boolean, default: 0 },
activity: { type: Boolean, default: 0 }
},
{
name : { type: String, default: 'foobar6'},
sent : { type: Boolean, default: 0 },
read : { type: Boolean, default: 0 },
activity: { type: Boolean, default: 0 }
},
{
name : { type: String, default: 'foobar7'},
sent : { type: Boolean, default: 0 },
read : { type: Boolean, default: 0 },
activity: { type: Boolean, default: 0 }
},
{
name : { type: String, default: 'foobar8'},
sent : { type: Boolean, default: 0 },
read : { type: Boolean, default: 0 },
activity: { type: Boolean, default: 0 }
}
]
});
當執行new User()
,將正確地創建所有內容,但lessons
數組除外 ,該數組以空數組形式返回,而不是使用其默認值創建嵌套對象。
可以在執行new User()
時創建這些對象,但是我希望這種情況在模型本身中發生,因為所有用戶將具有相同的lessons
和相同的初始值。
我怎樣才能做到這一點?
您可以在架構中添加預保存鈎子
schema.pre('save', function(next) {
if (this.isNew) {
this.set('lessons', [ ... ]);
}
next();
});
以下鏈接將有助於在貓鼬模式中設置嵌套數組的默認值。
您可以這樣:
var lessonSchema = Schema({
name: { type: String },
sent: { type: Boolean },
read: { type: Boolean },
activity: { type: Boolean }
});
var userSchema = Schema({
fbid: { type: String },
googleid: { type: String },
birthday: { type: String },
email: { type: String },
first_name: { type: String },
last_name: { type: String },
gender: { type: String },
age: { type: Number },
location: { type: String },
paid: { type: Boolean, default: 0 },
lessons: [lessonSchema]
});
userSchema.pre("save", function (next) {
if (!this.lessons || this.lessons.length == 0) {
this.lessons = [];
this.lessons.push({
"name": "football 1",
"sent": true,
"read": true,
"activity": true
})
this.lessons.push({
"name": "football 2",
"sent": true,
"read": true,
"activity": true
})
this.lessons.push({
"name": "football 3",
"sent": true,
"read": true,
"activity": true
})
this.lessons.push({
"name": "football 3",
"sent": true,
"read": true,
"activity": true
})
}
next();
});
在架構聲明中設置默認值:
var defaultLessons = [
{
name : 'foobar1',
sent : 0 ,
read : 0 ,
activity: 0
,
{
name : 'foobar2',
sent : 0 ,
read : 0 ,
activity: 0
,
{
name : 'foobar3',
sent : 0 ,
read : 0 ,
activity: 0
,
{
name : 'foobar4',
sent : 0 ,
read : 0 ,
activity: 0
,
{
name : 'foobar5',
sent : 0 ,
read : 0 ,
activity: 0
,
{
name : 'foobar6',
sent : 0 ,
read : 0 ,
activity: 0
,
{
name : 'foobar7',
sent : 0 ,
read : 0 ,
activity: 0
,
{
name : 'foobar8',
sent : 0 ,
read : 0 ,
activity: 0
}
];
var userSchema = new mongoose.Schema({
...
lessons: { type: [], default: defaultLessons}
});
另一種結構化方法是使用sub docs :
var lessonSchema = new mongoose.Schema({
name : { type: String, default: 'foobar'},
sent : { type: Boolean, default: 0 },
read : { type: Boolean, default: 0 },
activity: { type: Boolean, default: 0 }
});
var defaultLessons = [
{
name : 'foobar1',
},
{
name : 'foobar2',
},
{
name : 'foobar3',
},
{
name : 'foobar4',
},
{
name : 'foobar5',
},
{
name : 'foobar6',
},
{
name : 'foobar7',
},
{
name : 'foobar8',
}
];
var userSchema = new mongoose.Schema({
...
lessons: { type: [lessonSchema], default: defaultLessons}
});
但是,以上在貓鼬4+中不起作用,我已經嘗試過貓鼬3.8了。 看到這個討論 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.