node.js 猫鼬模式中的嵌套对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39596625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:34:01  来源:igfitidea点击:

Nested objects in mongoose schemas

node.jsmongodbmongoose-schema

提问by Samuel E.

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 "Customer", and it will have two fields that will have nested "subfields", and others that may repeat. here is what i mean:

我在这里看到了这个问题的很多答案,但我仍然不明白(也许是因为他们使用了更“复杂”的例子)......所以我试图做的是一个“客户”的模式,它将有两个具有嵌套“子字段”的字段,以及其他可能重复的字段。这就是我的意思:

let customerModel = new Schema({
    firstName: String,
    lastName: String,
    company: String,
    contactInfo: {
        tel: [Number],
        email: [String],
        address: {
            city: String,
            street: String,
            houseNumber: String
        }
    }   
});

teland emailmight be an array. and address will not be repeated, but have some sub fields as you can see.

telemail可能是一个数组。和地址不会重复,但有一些子字段,如您所见。

How can i make this work?

我怎样才能使这项工作?

回答by asissuthar

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/test');

var CustomerModel = mongoose.model('CustomerModel', {
    firstName: String,
    lastName: String,
    company: String,
    connectInfo: {
        tel: [Number],
        email: [String],
        address: {
            city: String,
            street: String,
            houseNumber: String
        }
    }
});

//create a record
var customer = new CustomerModel({
    firstName: 'Ashish',
    lastName: 'Suthar',
    company: 'asis',
    connectInfo: {
        tel: [12345,67890],
        email: ['[email protected]','[email protected]'],
        address: {
            city: 'x',
            street: 'y',
            houseNumber: 'x-1'
        }
    }
});

//insert customer object
customer.save((err,cust) => {
    if(err) return console.error(err);

    //this will print inserted record from database
    //console.log(cust);
});


// display any data from CustomerModel
CustomerModel.findOne({firstName:'Ashish'}, (err,cust) => {
    if(err) return console.error(err);

    //to print stored data
    console.log(cust.connectInfo.tel[0]); //output 12345
});


//update inner record
CustomerModel.update(
    {firstName: 'Ashish'},
    {$set: {"connectInfo.tel.0": 54320}}
    );

回答by esraa ali

// 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.