简体   繁体   English

如何在 mongoose 模式的项目数组中定义对象

[英]How can you define the object in an array of items for mongoose schema

is the best practice when creating a object inside of an array to simply define an object inside of an array?在数组内创建对象以简单地在数组内定义对象时,最佳实践是什么? How would mongodb know to expect an array, and would query on this array be efficent? mongodb 怎么知道需要一个数组,并且在这个数组上查询会有效?

for example in this schema例如在这个模式中

const vancouverSchema = new mongoose.Schema(
    {
        jobs: {
            retail: {
                type:Array
            },
            general_labour: {
                type:Array
            },
            sales: {
                type:Array
            },
            government: {

            }

         },
    }
)

would it be best practice to do simply最好的做法是简单地做

retail: [{
    title: {
        type: String
    },
    description: {
        type:String
    },
    pay: {
        type:Number
    }
}]

VS VS

retail: {
    type:Array
},

the attempt being to create a simple schema for a city and its various categories without much cognitive overhead for this part of the design.尝试为城市及其各种类别创建一个简单的模式,而这部分设计没有太多的认知开销。

This is an example of Vancouver Schema, but I will have 15-20++ city schema, simply copy and paste to create collections dynamically, and because there is so much to keep track of, it seems like the simplest way to do it.这是温哥华模式的一个示例,但我将有 15-20++ 个城市模式,只需复制和粘贴即可动态创建集合,而且由于要跟踪的内容太多,这似乎是最简单的方法。 I know, its bad to copy paste, but.. is it not permitted for high level design?我知道,复制粘贴不好,但是.. 高级设计不允许吗? rather than introduce bugs by trying some clever way, thanks everyone.而不是通过尝试一些聪明的方式来引入错误,谢谢大家。

const vancouverSchema = new mongoose.Schema(
    {
        jobs: {
            retail: [
            {
                title: {
                    type: String
                },
                description: {
                    type: String
                },
                pay: {
                    type: Number
                },
                contact_email: {
                    type: String
                }
            },
                {
        timestamps: true
                }

            ],
            general_labour: {
                type:Array
            },
            sales: {
                type:Array
            },
            government: {

            }

         },
        for_sale: {
            free: {
                type:Array
            },
            antiques: {
                type:Array

            },
            appliances: {
                type:Array

            }
        },
        housing:{
        apt: {
                type:Array
        },
        office_commerical:{
                type:Array
        }
        }
    }
)

You can do in this way:你可以这样做:

const vancouverSchema = new mongoose.Schema({
    jobs: {
        retail: [
            {
                type: mongoose.Schema.Types.ObjectId,
                ref: 'newSchemaCreated'
            }
        ],
        general_labour: {
            type: [Number]
        },
        sales: {
            type: [Boolean]
        },
        government: {}
    }
})

For retail would be better, if you create another schema.如果您创建另一个模式,零售会更好。 And if you have anything else like the retail property, you should do the same.如果您还有其他类似零售物业的东西,您也应该这样做。 Because put things too much nested is never good.因为把东西嵌套太多永远都不好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM