我有这样的架构:
const timeSchema = new Schema({
from: {
type: String,
validate: [
function (v) {
return TIME.test(v);
},
'{VALUE} must be a valid time'
],
required: true
},
to: {
type: String,
validate: [
{
validator: function (v) {
return TIME.test(v);
},
msg: '{VALUE} must be a valid time'
},
{
validator: function (v) {
return this.from < v;
},
msg: '{VALUE} must be greater than "from" time'
}
],
required: true
}
}, { _id: false });
const restaurantSchema = new Schema({
work_time: {
'0': {
type: timeSchema,
default: null
},
'1': {
type: timeSchema,
default: null
},
'2': {
type: timeSchema,
default: null
},
'3': {
type: timeSchema,
default: null
},
'4': {
type: timeSchema,
default: null
},
'5': {
type: timeSchema,
default: null
},
'6': {
type: timeSchema,
default: null
}
}
});
在创建新的Restaurant
文档时,它会正确设置工作时间,但在我尝试更新时会出现问题。
假设我创建了Restaurant
并设置了所有日期的时间,然后我想像这样更新一天:
Restaurant.findByIdAndUpdate(id, {
work_time: {
'0': {
from: '08:00',
to: '15:00'
}
}
})
所有其他日期都设为null
,为什么?
根据我的理解,无论出于何种原因,它都会设置默认值。