【问题标题】:Set strict value for a field in mongoose为猫鼬中的字段设置严格值
【发布时间】:2016-07-08 21:53:50
【问题描述】:

我想为 mongoose 中的字段设置严格值,例如在下面的模式中

var Option_Sets = new Schema({
    setId: {
        type: mongoose.Schema.ObjectId,
        default: mongoose.Types.ObjectId,
        required: true,
        dropDups: true,
        index: true
    },
    setName: { type: String },
    scope: String,
    components: [Schema.Types.Mixed]
});

我想设置范围字段只能在范围数组中取一个

var scopes = ["Global", "Store View", "Website"]; 

有人可以帮我举个例子并指导我如何做吗? 最好提前感谢

【问题讨论】:

    标签: mongoose mongoose-populate mongoose-schema


    【解决方案1】:

    为了执行这个尝试枚举:

    var Option_Sets = new Schema({
        setId: {
            type: mongoose.Schema.ObjectId,
            default: mongoose.Types.ObjectId,
            required: true,
            dropDups: true,
            index: true
        },
        setName: { type: String },
        scope: {
            type: String,
            enum: ["Global", "Store View", "Website"]
        },
        components: [Schema.Types.Mixed]
    });
    

    【讨论】:

    • 非常感谢..@B,Siscanu
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-05
    • 1970-01-01
    • 2019-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-26
    相关资源
    最近更新 更多