2016-04-30 57 views
0

我的猫鼬的模式是这样的:猫鼬模式:强制对象的数组要创建

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和相同的初始值。

我该如何做到这一点?

+0

它应该是'new mongoose.Schema(...)' –

回答

1

下面的链接将是对猫鼬架构嵌套阵列设置默认值的帮助。

set default values to mongoose arrays in node js

你可以这样做:在你的架构声明

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(); 
}); 
0

设置默认值:

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好。见this discussion