Mongoose,findByIdAndUpdate在更新时将嵌套模式设置为null

时间:2018-06-14 13:33:27

标签: mongoose mongodb-update

我有这样的架构:

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,为什么? 根据我的理解,无论出于何种原因,它都会设置默认值。

0 个答案:

没有答案