我有一个猫鼬模式,它的结构非常简单,还有一个是对象数组的字段,即子文档。当我的前端发布要添加的数据时,该请求中可能不会显示架构中的所有字段,并且可能并不总是存在要添加到此数组的子文档。子文档也将以status_value
的形式接收,然后后端会将其放入数组中。
var auditSchema = mongoose.Schema({
type: {
type: String,
default: ""
},
name: {
type: String,
default: ""
},
ui: {
type: String,
default: ""
},
account_number: {
type: String,
default: ""
},
lender: {
type: String,
default: ""
},
scanner_ui: {
type: String,
default: ""
},
lead_provider: {
type: String,
default: ""
},
lead_provider_ui: {
type: String,
default: ""
},
status: {
type: [{
status_value: {
type: String,
enum: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']
},
status_date: {
type: Date,
default: Date.now
}
}],
default: []
},
bill_date: Date,
requests: {
type: String,
default: ""
},
total_advanced: {
type: String,
default: ""
},
interest_overcharged: {
type: String,
default: ""
},
stat_interest_due: {
type: String,
default: ""
},
overpayment_due: {
type: String,
default: ""
},
term_of_loan: {
type: String,
default: ""
},
redeemed: {
type: String,
default: ""
},
date_audit_sent: Date,
}, {
strict: true,
});
当我做Audit.Create()
时,理想的情况是只做Audit.Create(req)
,但这不会正确地创建status
子文档。知道status_value
可能并不总是存在,然后应该创建一个空数组,这是创建子文档的最佳方法。