Javascript Mongoose模式:强制创建对象数组
我的猫鼬模式如下所示:Javascript Mongoose模式:强制创建对象数组,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,我的猫鼬模式如下所示: var userSchema = mongoose.Schema({ fbid : String, googleid : String, birthday : String, email : String, first_name : String, last_name : String, gender : String, age : Number, location : Str
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 }
}
]
});
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();
});
执行new User()
时,除了lessons
数组之外,所有内容都会正确创建,该数组返回为空数组,而不是使用默认值创建嵌套对象
在执行新建用户()
时,可以创建这些对象,但我希望在模型本身中发生这种情况,因为所有用户都将有相同的课程和相同的初始值
如何实现这一点?您可以在模式中添加预保存挂钩
schema.pre('save', function(next) {
if (this.isNew) {
this.set('lessons', [ ... ]);
}
next();
});
更多信息请参见此处您可以在模式中添加预保存挂钩
schema.pre('save', function(next) {
if (this.isNew) {
this.set('lessons', [ ... ]);
}
next();
});
下面的链接将有助于为mongoose模式中的嵌套数组设置默认值
你可以这样做:
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 }
}
]
});
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();
});
下面的链接有助于为mongoose模式中的嵌套数组设置默认值
你可以这样做:
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 }
}
]
});
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}
});
另一种更具结构的方法是使用:
但是,上面提到的在Mongoose4+中不起作用,我已经试过Mongoose3.8了。请参阅。在模式声明中设置默认值:
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}
});
另一种更具结构的方法是使用:
但是,上面提到的在Mongoose4+中不起作用,我已经试过Mongoose3.8了。请参阅。它应该是newmongoose.Schema(…)
它应该是newmongoose.Schema(…)