猫鼬用预定义的架构数据限制新文件

时间:2020-09-01 18:32:10

标签: node.js mongodb mongoose

我想创建一个成分模式,并且我想将 unit 字段限制为预定义的字段

const ingredientSchema = new mongoose.Schema({
    ingredientName: {
        type: String,
        required: true,
        min: 2,
        max: 255,
    },
    quantity: {
        type: {
            tpye: Number,
            required: true,
            min: 0
        }
    },
    unit: {
        type: mongoose.Schema.Types.ObjectId,
        ref:'Unit'
    }
});

所以我创建了单元架构

const unitSchema = new mongoose.Schema({
    unitName: {
        type: String,
        required: true,
        min: 2,
        max: 255,
    }
});

但是当我想将成分保存到Db时我会感到困惑。

 const ingredient = new Ingredient({
        ingredientName: req.body.ingredientName,
        quantity: req.body.quantity,
        unit: req.body.unit
    })

我应该在IngredientSchema的unit字段中发送unit的_id,并且要提取带有_id的UnitName的成分数据检查unitName还是有其他方法

0 个答案:

没有答案
相关问题