MongooseJS - 如何定义自定义类型

时间:2018-03-21 03:37:24

标签: javascript mongoose mongoose-schema

使用" mongoose":" 4.9.8",我想定义一致性和重用的类型。 我知道这个概念,但是我对语法有些不满,比如说我的mongoose架构看起来像这样

const thingsSchema = new Schema({
    thing: {
        type: String
        ,required: false
    }   
    ,location: {
        city: {
            type: String
            ,required: false
        }
        ,state: {
            type: String
            ,required: false
        }
        ,country: {
            type: String
            ,required: false
        }
    }   
});

我真正想做的是使用"位置"型号/类型 在其他地方再次,并没有要求的位置...像这样:

const thingsSchema = new Schema({
    thing: {
        type: String
        ,required: false
    }   
    ,location: {
        type: Location
        ,required: false
    }   
});

我认为这样做了 - 但我不确定。

const Location: {
        city: {
            type: String
            ,required: false
        }
        ,state: {
            type: String
            ,required: false
        }
        ,country: {
            type: String
            ,required: false
        }
    };

0 个答案:

没有答案
相关问题