猫鼬封顶的子阵

时间:2017-08-26 12:42:36

标签: node.js mongodb mongoose mongoose-schema

我可以创建这样的上限模式: -

var deviceSchema = new mongoose.Schema({
    name: String,
    vendor: String,
    type: String,
    backups: [{
        date: String,
        rawbackup: String
    }]
}, {
    capped: true,
    size: 10485760,
    max: 20
})

但我想只限制备份数组。我试过这个: -

var deviceSchema = new mongoose.Schema({
    name: String,
    vendor: String,
    type: String,
    backups: [{
            date: String,
            rawbackup: String
        },
        {
            capped: true,
            size: 10485760,
            max: 20
        }
    ]
})
它还在增长。我如何限制一个子阵列?

1 个答案:

答案 0 :(得分:0)

var deviceSchema = new mongoose.Schema({
            name: String,
            vendor: String,
            type: String,
            backups: [{
                date: String,
                rawbackup: String,
                validate: arrayLimit
            }]
        },

        function arrayLimit(val) {
            return val.length <= 20;
        }

您可以设置架构validation