2
我試圖在一個空字段設置默認值的文件被保存在MongoDB中貓鼬:在必填字段設置默認值時,這是不存在的文件中保存
在我的情況下,這是貓鼬架構
var Distribution = new Schema({
temporalCoverage : { startDate : { type: Date, default: Date.now }, endDate : { type: Date, default: Date.now } },
distributionText : String
});
這是文件保存:
在這份文件中,空字段結束日期且StartDate。該文檔以空值保存在mongoDB中。
"distribution": {
"temporalCoverage": {
"endDate": null,
"startDate": null
},
"distributionText": "test"
}
我要保存的默認值,而不是空
如果把temporalCoverage : { startDate : { type: Date, required: true, default: Date.now }, endDate : { type: Date, required: true, default: Date.now } }
在架構所需的領域,我得到的錯誤驗證和文檔沒有保存的它。
更新時,儘量不提供這些密鑰。不要將它們設置爲空字符串。 – Edgar