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
}
所以我想知道它是如何可能的。任何方向都将受到赞赏
答案 0 :(得分:0)
是的,有可能!!这就是文档数据库允许你做的事情。
根据您的架构,患者架构有一系列紧急联系人
因此,newPatient.EmergencyContacts.emContact_Address
和newPatient.EmergencyContacts.emContact_Info
会给您错误,因为 newPatient.EmergencyContacts 是一个数组。
因此,您无法访问必须提供给您的紧急联系人数组的 emContact_Address 和 emContact_Info 属性 undefined
虽然您可以通过EmergencyContacts上的索引访问它:
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
});
希望它有所帮助!!
建议: 1)请让其他用户可以阅读您的问题。
2)您可以重构您的架构设计,请访问其他设计 mongoose / mongodb的功能类似于更新,$ push,$ elemMatch 的组合 等,以减少您的代码长度
快乐编码:)
答案 1 :(得分:0)
SCHEMA
var mongoose = require('mongoose') ,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}
代码
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
});
}
});
}