2015-07-22 51 views
-1

http://mongoosejs.com/docs/subdocs.html嵌套子文件猫鼬......这是可能

我使用上面的链接,以“推”儿童的数据为父母..

我可以把数据推抱孙子到孩子和家长进入?

CODE

newPatient.EmergencyContacts.push({ 
    emContact_Name: emContact_Name, 
    emContact_Relation: emContact_Name, 
}) 

newPatient.EmergencyContacts.emContact_Address.push({ 
    emContact_AddressType: emContact_AddressType, 
    emContact_AddressLine1: emContact_AddressLine1, 
    emContact_AddressLine2: emContact_AddressLine2, 
    emContact_City: emContact_City, 
    emContact_Town: emContact_Town, 
    emContact_Village: emContact_Village, 
    emContact_PolicStation: emContact_PolicStation, 
    emContact_District: emContact_District, 
    emContact_State: emContact_State, 
    emContact_PinCode: emContact_PinCode 
}); 

newPatient.EmergencyContacts.emContact_Info.push({ 
    emContact_phone: emContact_phone, 
    emContact_email: emContact_email 
}); 

SCHEMA

var mongoose = require('mongoose') 
, Schema = mongoose.Schema; 


var emContactInfoSchema = new Schema({ 
     emContact_Phone: String, 
     emContact_Email: String, 
}); 

var emContactAddressSchema = new Schema({ 
     emContact_AddressType: String, 
     emContact_AddressLine1: String, 
     emContact_AddressLine2: String, 
     emContact_City: String, 
     emContact_Town: String, 
     emContact_Village: String, 
     emContact_PolicStation: String, 
     emContact_District: String, 
     emContact_State: String, 
     emContact_PinCode: Number, 
}); 


var emContactSchema = new Schema({ 
     emContact_Name: String, 
     emContact_Relation: String, 
     emContact_Address: [emContactAddressSchema], 
     emContact_Info: [emContactInfoSchema], 
}); 

var patientAddressSchema = new Schema({ 
     Patient_addressType: String, 
     Patient_addressLine1: String, 
     Patient_addressLine2: String, 
     Patient_city: String, 
     Patient_town: String, 
     Patient_village: String, 
     Patient_policeStation: String, 
     Patient_district: String, 
     Patient_state: String, 
     Patient_pinCode: Number, 
     Patient_countryCode: String, 
}); 

var patientContactInfoSchema = new Schema({ 
     Patient_phoneType: String, 
     Patient_phoneNumber: String, 
     Patient_emailType: String, 
     Patient_email: String, 
}); 

var patientSchema = new Schema({ 
    // patient info 
    Patient_UHID: { type: String, required: true, index: { unique: true } } , // Univesal Health Indentifier - Aadhar in our case 
    Patient_altUHID: { type: String, required: false, trim: true, index: { unique: false } }, // As per institution or vendor's specifications 
    Patient_fName: String, 
    Patient_mName: String, 
    Patient_lName: String, 
    Patient_dob: Date, 
    Patient_age: Number, 
    Patient_gender: String, 
    Patient_occupation: String, 

    Patient_Addresses : [patientAddressSchema], 

    Patient_ContactInfo: [patientContactInfoSchema], 

    Patient_insuranceStatus: String, 
    Patient_allergyStatus: String, 

    // Emergency Contact info 
    EmergencyContacts: [emContactSchema], 
}); 

var patient = mongoose.model('patient', patientSchema); 
module.exports = { 
    Patient: patient 
} 

所以我想知道它是如何possible.Any方向可以理解

+2

你试试...? – robertklep

+0

是的,我试过不工作....所以这就是为什么我要问,如果猫鼬支持 –

+0

那么你是怎样尝试,到底是什么?你能显示一些代码吗? – robertklep

回答

0

是的,它是可能的!这就是文档数据库允许您执行的操作。

按照经历给您的模式,患者架构已紧急触点阵列 所以newPatient.EmergencyContacts.emContact_AddressnewPatient.EmergencyContacts.emContact_Info会给你错误,因为newPatient.EmergencyContacts是一个数组。

因此,你不能访问EmergencyContacts数组肯定会为您提供undefined

虽然你可以访问通过索引上EmergencyContacts的emContact_AddressemContact_Info属性:

newPatient.EmergencyContacts[0].emContact_Address.push({ 
              emContact_AddressType: emContact_AddressType, 
              emContact_AddressLine1: emContact_AddressLine1, 
              emContact_AddressLine2: emContact_AddressLine2, 
              emContact_City: emContact_City, 
              emContact_Town: emContact_Town, 
              emContact_Village: emContact_Village, 
              emContact_PolicStation: emContact_PolicStation, 
              emContact_District: emContact_District, 
              emContact_State: emContact_State, 
              emContact_PinCode: emContact_PinCode 
              }); 
     newPatient.EmergencyContacts[0].emContact_Info.push({ 
               emContact_phone: emContact_phone, 
               emContact_email: emContact_email 

    }); 

希望它可以帮助!

Suggestions : 1)Please make your question readable for other users.

2)You can refactor your schema design and please go though other mongoose/mongodb functions like combination of update,$push,$elemMatch etc to reduce your code length

快乐编码:)

+0

感谢....这有助于 –

+0

您的欢迎:) –

+0

我居然发现还有另一种方法做同样的事情... –

0

SCHEMA

变种猫鼬=要求( '猫鼬') ,模式= mongoose.Schema;

var emContactSchema = new Schema({ 
             emContact_Name: String, 
             emContact_Relation: String, 
             emContact_Address: [], 
             emContact_Info: [], 
           }); 




var patientAddressSchema = new Schema({    Patient_addressType: String,  
                 Patient_addressLine1: String, 
              Patient_addressLine2: String, 
            Patient_city: String, 
            Patient_town: String, 
            Patient_village: String, 
            Patient_policeStation: String, 
            Patient_district: String, 
            Patient_state: String, 
            Patient_pinCode: Number, 
            Patient_countryCode: String, 
           }); 

var patientContactInfoSchema = new Schema({ 
             Patient_phoneType: String, 
             Patient_phoneNumber: String, 
             Patient_emailType: String, 
             Patient_email: String, 
            }); 

var patientSchema = new Schema({ 

// patient info 
Patient_UHID: { type: String, required: true, index: { unique: true } } , // Univesal Health Indentifier - Aadhar in our case 
Patient_altUHID: { type: String, required: false, trim: true, index: { unique: false } }, // As per institution or vendor's specifications 
Patient_fName: String, 
Patient_mName: String, 
Patient_lName: String, 
Patient_dob: Date, 
Patient_age: Number, 
Patient_gender: String, 
Patient_occupation: String, 

Patient_Addresses : [patientAddressSchema], 

Patient_ContactInfo: [patientContactInfoSchema], 



Patient_insuranceStatus: String, 
Patient_allergyStatus: String, 

// Emergency Contact info 
EmergencyContacts: [emContactSchema], 




}); 

var patient = mongoose.model('patient', patientSchema); 
module.exports = { 
Patient: patient} 

CODE

Patient.findOne({ 
Patient_UHID: { 
    $regex: new RegExp(Patient_UHID, "i") 
} 
}, function(err, doc) { // Using RegEx - search is case insensitive 
if (!err && !doc) { 

    var newPatient = new Patient(); 


    newPatient.Patient_UHID = Patient_UHID 
    newPatient.Patient_altUHID = Patient_altUHID 
    newPatient.Patient_fName = Patient_fName 
    newPatient.Patient_mName = Patient_mName 
    newPatient.Patient_lName = Patient_lName 
    newPatient.Patient_dob = Patient_dob 
    newPatient.Patient_age = Patient_age 
    newPatient.Patient_gender = Patient_gender 
    newPatient.Patient_occupation = Patient_occupation 
    newPatient.Patient_Addresses.push({ 
             Patient_addressType: Patient_addressType, 
             Patient_addressLine1: Patient_addressLine1, 
             Patient_addressLine2: Patient_addressLine2, 
             Patient_city: Patient_city, 
             Patient_town: Patient_town, 
             Patient_village: Patient_village, 
             Patient_policeStation: Patient_policeStation, 
             Patient_district: Patient_district, 
             Patient_state: Patient_state, 
             Patient_pinCode: Patient_pinCode, 
             Patient_countryCode: Patient_countryCode 
            }); 
    newPatient.Patient_ContactInfo.push({ 
             Patient_phoneType: Patient_phoneType, 
             Patient_phoneNumber: Patient_phoneNumber, 
             Patient_emailType: Patient_emailType, 
             Patient_email: Patient_email 
             }); 

    newPatient.Patient_insuranceStatus = Patient_insuranceStatus; 
    newPatient.Patient_allergyStatus = Patient_allergyStatus; 



    newPatient.EmergencyContacts.push({ 
             emContact_Name: emContact_Name, 
             emContact_Relation: emContact_Name, 
             emContact_Address: [{ 
             emContact_AddressType: emContact_AddressType, 
             emContact_AddressLine1: emContact_AddressLine1, 
             emContact_AddressLine2: emContact_AddressLine2, 
             emContact_City: emContact_City, 
             emContact_Town: emContact_Town, 
             }], 
             emContact_Info: [{ 
                  emContact_Phone: emContact_Phone, 
                  emContact_Email: emContact_Email, 
                 }] 


    }); 




    newPatient.save(function(err) { 

    if (!err) { 
     res.status(201).json({ 
     message: "Patient created with Patient_UHID: " + newPatient.Patient_UHID 
     }); 
    } else { 
     res.status(500).json({ 
     message: "Could not create patient. Error: " + err 
     }); 
    } 

    }); 

} else if (!err) { 

    // User is trying to create a patient with a Patient_UHID that already exists. 
    res.status(403).json({ 
    message: "Patient with that Patient_UHID already exists, please update instead of create or create a new patient with a different Patient_UHID." 
    }); 

} else { 
    res.status(500).json({ 
    message: err 
    }); 
} 
}); 

}