【问题标题】:nesting sub documents mongoose ... Is this possible嵌套子文档猫鼬...这可能吗
【发布时间】:2015-10-11 20:45:21
【问题描述】:

http://mongoosejs.com/docs/subdocs.html

我正在使用上面的链接将孩子的数据“推送”给父母..

我可以将孙子的数据推送给孩子和父母吗?

代码

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
});

架构

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
}

所以我想知道这是怎么可能的。任何方向都将不胜感激

【问题讨论】:

  • 是的,我试过没用....所以我问猫鼬是否支持
  • 那么你是怎么尝试的呢?你能显示一些代码吗?
  • 当尝试发布它给我错误说 newPatient.EmergencyContacts.emContact_Address.push({ ^ TypeError: Cannot read property 'push' of undefined
  • 我已将 'EmergencyContacts' 和 'emContact_Address' 声明为 mongoose 模型中的数组
  • 你也应该发布你的架构

标签: node.js mongoose


【解决方案1】:

是的,这是可能的!这就是文档数据库允许您做的事情。

根据您的架构,患者架构有一系列紧急联系人 所以newPatient.EmergencyContacts.emContact_AddressnewPatient.EmergencyContacts.emContact_Info 会给你错误,因为 newPatient.EmergencyContacts 是一个数组。

因此,您无法访问 EmergencyContacts 数组的 emContact_AddressemContact_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 函数,例如 update,$push,$elemMatch 的组合 等以减少您的代码长度

快乐编码:)

【讨论】:

  • 愿意分享.. :)
  • 当然...在这里...我尝试使用 chrome 中的高级 REST 客户端将数据发布到嵌套数组,它正在工作!
  • 我会推荐你​​使用 $push 通过更新命令。它还将增强您的查找查询功能,因为您将使用 $elemMatch。 :)
【解决方案2】:

架构

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
  });
}
});

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-05
    • 1970-01-01
    • 2023-01-31
    • 2016-11-09
    • 2016-06-08
    • 1970-01-01
    • 2020-08-18
    相关资源
    最近更新 更多