Nested objects in mongoose schemas

前端 未结 2 1134
轮回少年
轮回少年 2020-12-23 14:33

i\'ve seen many answers to this question here, but i still don\'t get it (maybe because they use more \"complex\" examples)... So what im trying to do is a schema for a \"Cu

2条回答
  •  [愿得一人]
    2020-12-23 15:10

    // address model
        var addressModelSchema = new Schema({
            city: String,
            street: String,
            houseNumber: String
        })
        mongoose.model('address',addressModelSchema ,'address' )
    
    // contactInfo model
        var contactInfoModelSchema = new Schema({
            tel: [Number],
            email: [String],
            address: {
                type: mongoose.Schema.Type.ObjectId,
                ref: 'address'
            }
        })
        mongoose.model('contactInfo ',contactInfoModelSchema ,'contactInfo ')
    
    // customer model
        var customerModelSchema = new Schema({
            firstName: String,
            lastName: String,
            company: String,
            contactInfo: {
                type: mongoose.Schema.Type.ObjectId,
                ref: 'contactInfo'
            }  
        });
        mongoose.model('customer', customerModelSchema, 'customer')
    
    // add new address then contact info then the customer info
    // it is better to create model for each part.
    

提交回复
热议问题