Mongoose不会让我插入空值的数字?

我定义了下面的Mongoose模式:

participantSchema = new Schema({ id: Number, email: String, survey: { type: Schema.Types.ObjectId, ref: 'Survey' }, created: { type: Date, "default": Date.now }, answers: [ { question: { type: Schema.Types.ObjectId, ref: 'Question' }, values: Array, question_id: Number, sub_id: Number } ] }); 

当我尝试插入我的answers数组的下列值:

 [{ question_id: 60800, _id: 52f53345f06cf301f2a38465, values: [ 'c2' ] } { question_id: 60801, sub_id: 19690, _id: 52f53345f06cf301f2a38464, values: [ 'C1' ] } { question_id: 60801, sub_id: 19691, _id: 52f53345f06cf301f2a38463, values: [ 'C3' ] } { question_id: 60802, _id: 52f53345f06cf301f2a38462, values: [ null ] } { question_id: 60803, sub_id: 19692, _id: 52f53345f06cf301f2a38461, values: [ null ] }] 

它引发错误:

CastError:在path“sub_id”上投射到编号失败的值为“null”

如果我硬编码所有的sub_id为1或null它似乎工作,但是当有混合它会引发错误。 任何build议工作?

做这个:

 sub_id: {type: Number, sparse: true} 

并检查了这一点