2014-02-07 33 views
2

我有以下的貓鼬架構中定義:Mongoose不會讓我爲Number插入空值嗎?

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: Cast to number failed for value "null" at path "sub_id"

如果我將所有sub_id s硬編碼爲1或null,它似乎工作,但是當有混合它會引發錯誤。任何建議爲工作?

回答

2

這樣做:

sub_id: {type: Number, sparse: true} 

,並檢查了this

+0

是用於設置索引 '疏'?這只是爲了獲得有效的號碼插入嗎? (好奇知道) – tonejac

相關問題