当记录添加到集合时,Mongoose将默认Array对象添加到Sub Array

时间:2016-09-04 14:09:27

标签: javascript mongodb mongoose database-schema

我想在将记录添加到集合时将Array对象添加到Sub数组作为默认值。请在下面找到我的chema

const JobSpecificetionSchema = new Schema({
 ACTIVITY:[{
    DESCRIPTION: { type: String, default: '' },
    SUB_ACTIVITY:[{
      DESCRIPTION: { type: String, default: '' },
      REPEAT_TYPE: { type: String, default: '' },// 1 - inspection will start 2 - inspection will not start
      FREQUENCY: { type: String, default: '' }, // 1 - weekly, 2 - monthly, 3 - yearly
      PAR_1: { type: String, default: '' }, // if weekly, then every one week or two. then monthly every one month or two months
      PAR_2: { type: String, default: '' }, // if weekly then days in the week. ie (Monday 1, tue 2) (1,2,3)  Monthly- day ie 25  yearly- month ie 1 to 12
      PAR_3: { type: String, default: '' }, // if yearly then day 25
      ASSIGNEE_WORKERS:[{
        ASSIGNEE_ID : { type: Schema.Types.ObjectId, default: '000000000000000000000000' },
      }]
    }]
  }]

});

当我为此集合创建记录时,您可以看到我想向 ASSIGNEE_WORKERS 数组添加默认数组。

目前的方式并不奏效。请帮助这个

1 个答案:

答案 0 :(得分:0)

你可以使用“预存”钩子并在那里分配你的默认数组,如果它是一个新文件(文件上的isNew属性,文件本身可以在预保存钩子中作为“this”访问),如果这是什么你想要的。