Mongoose模式:强制创建一个对象数组
Mongoose Schema: Forcing an array of objects to be created
我的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 }
}
]
});
执行new User()
时,除了lessons
数组的之外,所有内容都会正确创建,该数组将作为空数组返回,而不是使用默认值创建嵌套对象。
在执行new User()
时可以创建这些对象,但我希望这种情况发生在模型本身中,因为所有用户都将具有相同的lessons
和相同的初始值。
我怎样才能做到这一点?
以下链接将有助于在mongoose Schema中设置嵌套数组的默认值。
在节点js 中为mongoose数组设置默认值
你可以这样做:
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();
});
您可以在模式中添加预保存挂钩
schema.pre('save', function(next) {
if (this.isNew) {
this.set('lessons', [ ... ]);
}
next();
});
更多信息请点击此处http://mongoosejs.com/docs/middleware.html
在架构声明中设置默认值:
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}
});
另一种更结构化的方式是使用子文档:
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 ok。请参阅本文讨论。
相关文章:
- Mongoose模式:强制创建一个对象数组
- 是否可以创建一个对象,该对象是另一个对象的子集,而不会丢失javascript中的引用
- 有没有一种方法可以创建一个对象并同时为其指定一个动态特性
- javascript创建一个对象来打印数组对象
- 你能在Javascript中创建一个对象而不声明每个值吗,以及如何
- 如何创建一个对象,其中对象本身是一个函数
- 使用拆分字符串的数组创建一个对象
- Javascript:创建一个对象构造函数
- 如何测试 javascript 方法是否创建一个对象并调用该对象上的方法
- 使用变量创建一个对象文字,如何只获取其值
- 从JSON创建一个对象,并在Javascript中给它一些方法
- 我可以在js中通过父对象创建一个对象吗
- 解析字符串以创建一个对象数组,javascript
- 如何创建一个对象的多个实例,而不在JavaScript中重用相同的变量名
- 从mongoose数据库创建一个对象数组
- 基于另外两个对象创建一个对象
- 从字符串创建一个对象.种
- 在 JS 中创建一个对象
- 如何在JavaScript中使用For-Each循环来创建一个对象,它的键和值是动态提供的
- 我应该使用new在typescript类中创建一个对象属性吗?