【问题标题】:how to set mongoose schema for select option list and set property on selected option如何为选择选项列表设置猫鼬模式并在选定选项上设置属性
【发布时间】:2020-08-29 07:05:37
【问题描述】:
const mongoose = require('mongoose')

const Schema = mongoose.Schema()

const productSchema = new Schema(
    {
        name : {
            type: String,
            required : true
        },
        price : {
            type : Number,
            required : true 
        },

此处引用选择单位选项,例如 - 公斤、升、米、厘米

    }
)

【问题讨论】:

    标签: node.js mongodb mongoose schema


    【解决方案1】:

    Mongoose 没有选择类型,您需要管理视图中的下拉菜单,但您应该定义一组属性,以便该字段仅接受这些属性,ENUM

    const mongoose = require('mongoose')
    
    const Schema = mongoose.Schema()
    
    const productSchema = new Schema(
        {
            name : {
                type: String,
                required : true
            },
            price : {
                type : Number,
                required : true 
            },
            units: {
                type: String,
                enum: ['KG', 'liters', 'meters', 'cm'],
                required : true 
            }
        }
    )
    

    【讨论】:

    • 明白了.. 我可以在前端将枚举列表作为数组访问吗?
    • 从 MongoDB 架构你不能在前端得到它,你需要在你的配置中手动管理它。
    猜你喜欢
    • 1970-01-01
    • 2015-03-22
    • 2015-08-27
    • 2014-11-18
    • 1970-01-01
    • 2011-03-31
    • 2020-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多