2016-09-20 18 views
7

Burada bu soruya birçok yanıt gördüm, ama hala anlamadım (belki de daha "karmaşık" örnekler kullanıyor olmaları nedeniyle) ... Ne yapmaya çalışıyorum? Bir "Müşteri" şeması ve iç içe geçmiş "alt alanlar" ve tekrarlayabilen diğer iki alanı olacaktır.Moğol şemasında iç içe geçmiş nesneler

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

tel ve e bir dizi olabilir: Burada ne demek budur. ve adres tekrarlanmayacak, ancak görebileceğiniz gibi bazı alt alanlar var.

Bu işi nasıl yapabilirim?

cevap

5
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}} 
    ); 
+0

Bu nasıl yapılacağına ilişkin herhangi bir yorum: https://stackoverflow.com/questions/48753436/dynamic-mongodb-schema-object-creation-in-angularjs – CodeHunter

2
// 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. 
+0

bu konuda herhangi bir yorum: https://stackoverflow.com/questions/48753436/dynamic-mongodb-schema-object-creation-in-angularjs – CodeHunter

İlgili konular