【问题标题】:Mongoose Schema: Forcing an array of objects to be createdMongoose Schema:强制创建对象数组
【发布时间】:2016-04-30 03:09:59
【问题描述】:

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

我怎样才能做到这一点?

【问题讨论】:

  • 应该是new mongoose.Schema(...)

标签: javascript node.js mongodb mongoose


【解决方案1】:

以下链接有助于在 mongoose Schema 中设置嵌套数组的默认值。

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

【讨论】:

    【解决方案2】:

    您可以在架构中添加预保存挂钩

    schema.pre('save', function(next) {
      if (this.isNew) {
        this.set('lessons', [ ... ]);
      }
      next();
    });
    

    更多信息在这里http://mongoosejs.com/docs/middleware.html

    【讨论】:

      【解决方案3】:

      在你的架构声明中设置默认值:

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

      但是,上述方法在 mongoose 4+ 中不起作用,我已经尝试过 mongoose 3.8 好的。见this discussion

      【讨论】:

        猜你喜欢
        • 2020-04-20
        • 1970-01-01
        • 2014-05-26
        • 2020-04-07
        • 2016-02-26
        • 2017-10-15
        • 1970-01-01
        • 2018-02-28
        相关资源
        最近更新 更多